![](/img/trans.png)
[英]How to change the background color of tkinter ttk.PanedWindow widget?
[英]how to change the color of PanedWindow upon hovering over it, for multiple PanedWindow in tkinter?
當我在 hover 中將鼠標懸停在 tkinter 上時,我試圖讓 PanedWindow 改變顏色。
現在這適用於單次迭代。 但是當我嘗試為多個窗格窗口執行此操作時,它只會更改最后一個 window 的顏色。
import tkinter as tk
root = tk.Tk()
for i in range(10):
m1 = tk.PanedWindow(root, bd=4, relief="flat", bg="blue")
m1.pack()
def on_enter(e):
m1.config(background='OrangeRed3', relief="flat")
def on_leave(e):
m1.config(background='SystemButtonFace', relief="flat")
# Create a Button
button1 = tk.Button(m1, text=f"{i}")
button1.pack(pady=20)
# Bind the Enter and Leave Events to the Button
m1.bind('<Enter>', on_enter)
m1.bind('<Leave>', on_leave)
m1.add(button1)
tk.mainloop()
由於在循環的每次迭代中,所有變量都被覆蓋,函數被綁定到最后創建的元素。 有必要將所需的元素傳遞給 function。最好將在字典中創建的所有內容收集起來,以便將來您可以輕松更改它們。
import tkinter as tk
from functools import partial
ms = {}
btns = {}
root = tk.Tk()
def on_enter(m, e):
m.config(background='OrangeRed3', relief="flat")
def on_leave(m, e):
m.config(background='SystemButtonFace', relief="flat")
for i in range(10):
ms[i] = tk.PanedWindow(root, bd=4, relief="flat", bg="blue")
ms[i].pack()
# Create a Button
btns[i] = tk.Button(ms[i], text=f"{i}")
btns[i].pack(pady=20)
# Bind the Enter and Leave Events to the Button
ms[i].bind('<Enter>', partial(on_enter, ms[i]))
ms[i].bind('<Leave>', partial(on_leave, ms[i]))
ms[i].add(btns[i])
tk.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.