gpt4 book ai didi

python - 无法使 Ttk 样式映射工作

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:01 24 4
gpt4 key购买 nike

所以我环顾四周,但关于样式的问题很少,但没有一个回答。

我无法使样式映射工作。我不知道我错过了什么。如果您能纠正我,那就太好了,谢谢。

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

s = ttk.Style()
s.map("C.TFrame",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])

frame = ttk.Frame(root, style="C.TFrame")
text = ttk.Label(frame, text="This is some really long text\n123...")

frame.grid()
text.grid()

root.mainloop()

最佳答案

框架样式不响应单击事件和鼠标悬停(悬停)事件。按钮可以。请参阅下面的代码并尝试一下。我还这样做了,以便文本小部件以您尝试使框架响应的方式响应事件。由于文本小部件不是主题小部件,因此无法使用样式对其进行配置,但您可以使用 tk options 与此类似地配置您的应用程序,并将其保存在单独的文件中。但这是一个不同的故事。

def configureTextWindow(**kwargs):
for avp in kwargs.items():
attrib, value = avp
text[attrib] = value

s = ttk.Style()
# This won't work because frames don't respond to style events.
s.map("C.TFrame",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])
# Does work because buttons DO respond to style events.
s.map("C.TButton",
foreground=[("pressed", "red"), ("active", "blue")],
background=[("pressed", "!disabled", "black"), ("active", "white")])

frame = ttk.Frame(root, style="C.TFrame")
button = ttk.Button(frame, style='C.TButton', text='Press or Hover')
button.grid()
text = ttk.Label(frame, text="This is some really long text\n123...")
frame.grid()
text.grid()
# Force configuration on the text widget that mimics the frame style above.
text.bind('<Enter>', lambda event: configureTextWindow(foreground='blue', background='white'))
text.bind('<Leave>', lambda event: configureTextWindow(foreground='black', background=''))
text.bind('<Button-1>', lambda event: configureTextWindow(foreground='red', background='black'))
text.bind('<ButtonRelease-1>', lambda event: configureTextWindow(foreground='blue', background='white'))
root.mainloop()

关于python - 无法使 Ttk 样式映射工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com