简体   繁体   English

TTK 按钮 - 如何使其处于“突出显示”状态?

[英]TTK Button - How to make it in "highlighted" state?

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:见附图:

在此处输入图像描述

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?如果不可能,是否可以更改按钮颜色以使其处于“突出显示”状态时?

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM