这里需要 tkinter 代码来更新鼠标悬停时的颜色。
不同的 tkinter 小部件用作具有不同选项的按钮
use_ttk_buttons
-
使用选项
use_ttk_buttons=False
,它将使用tkinter Button 作为sg.Button,并且可以通过
configure
的
element.Widget
的方法
configure
更新选项
activeforeground
和
activebackground
-
使用选项
use_ttk_buttons=True
,它将使用 tkinter.ttk 按钮作为 sg.Button,并且可以使用 tkinter.ttk 样式更新相关选项。
这是演示代码
import PySimpleGUI as sg
def update(element, colors):
widget = element.Widget
if isinstance(widget, sg.tk.Button):
widget.config(activeforeground=colors[0], activebackground=colors[1])
else:
style_name = widget.configure()["style"][-1]
style = sg.ttk.Style()
style.map(style_name, foreground=[('active', colors[0])], background=[('active', colors[1])])
index = 0
color = {0: ("white", "blue"), 1: ("yellow", "green")}
font = ("Courier New", 16)
sg.theme("DarkBlue3")
sg.set_options(font=font)
layout = [[sg.Button("Hello", mouseover_colors=color[index], use_ttk_buttons=True)]]
window = sg.Window("test", layout, finalize=True)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'Hello':
index = 1 - index
update(window['Hello'], color[index])
window.close()