0

below a simple code, it's just a button widget placed in a window:

import tkinter as tk
from tkinter import ttk

class MainWindow:
    def __init__(self):
        self.parent=tk.Tk()
        self.parent.geometry("494x410+370+100")
        self.parent.title("Test")

        Button = ttk.Button(self.parent, text="My Button", command=self.DoNothing)
        Button.place(x=16, y=16)

        self.parent.mainloop()
    
    def DoNothing(self):
        pass

if __name__=="__main__":
    app=MainWindow()

is it possible to put this button in "highlighted" state? I mean, is it possibile to change its color like when the mouse cursor stays on it? see the attached image:

enter image description here

in my real software, I have some buttons. all of them open a different menu and when I'm working in one of them, I would very like to remember in which menu I am. to do that I thought to put the button in "highlighted" state and come back to the original one when I click on another button (when I change the menu).

if it is not possible, is there the possibility to change the button colors to make it like when it is in "highlighted" state?

TurboC
  • 777
  • 1
  • 5
  • 19
  • I don't think `ttk` has something out of the box for that. However, their `ThemedStyle` has a theme called `plastik` that will place a dotted border around the button after it is pressed and that might be something that could possibly work. I use it in an application that launches another window and the dotted border stays dotted until you go back to the same window the button was in and change focus to another widget. – Rory May 23 '22 at 23:46

2 Answers2

0

is it possible to put this button in "highlighted" state? I mean, is it possibile to change its color like when the mouse cursor stays on it? see the attached image:

Added four ttk.styles in script:

  1. self.style = ttk.Style()
  2. self.style.theme_use('alt')
  3. self.style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none')
  4. self.style.map('TButton', background=[('active','green')])

Snippet:

import tkinter as tk
from tkinter import ttk

class MainWindow:
    def __init__(self):
        self.parent=tk.Tk()
        self.parent.geometry("494x410+370+100")
        self.parent.title("Test")
        self.style = ttk.Style()
        self.style.theme_use('alt')
        self.style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none')
        self.style.map('TButton', background=[('active','green')])

        Button = ttk.Button(self.parent, text="My Button", command=self.DoNothing)
        Button.grid()

        self.parent.mainloop()
    
    def DoNothing(self):
        pass

if __name__=="__main__":
    app=MainWindow()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19
-2
import tkinter as tk


class MainWindow:
    def __init__(self):
        def hover_on(e, color):
            Button['activebackground'] = color

        def hover_off(e):
            Button['activebackground'] = self.default_color

        self.parent = tk.Tk()
        self.parent.geometry("494x410+370+100")
        self.parent.title("Test")

        Button = tk.Button(self.parent, text="My Button", command=self.DoNothing)
        Button.place(x=16, y=16)
        self.default_color = Button['background']

        Button.bind('<Enter>', lambda e: hover_on(e, 'red'))
        Button.bind('<Leave>', lambda e: hover_off(e))

        self.parent.mainloop()

    def DoNothing(self):
        pass


if __name__ == "__main__":
    app = MainWindow()
Ze'ev Ben-Tsvi
  • 1,174
  • 1
  • 3
  • 7