![](/img/trans.png)
[英]how to change the color of PanedWindow upon hovering over it, for multiple PanedWindow in tkinter?
[英]How do you change the state of multiple tkinter buttons when your mouse is pressed and hovering over it?
我有一個二維 tkinter 按鈕數組。
我希望能夠單擊一個按鈕,按住鼠標,然后在按下鼠標時懸停在上面的每個按鈕都會更改 colors。 到目前為止,我已經達到了這樣的程度,如果您在任何正方形上使用 hover,無論是否按下鼠標按鈕,它都會更改 colors。
到目前為止,代碼看起來像這樣:
def draw(self, i, j):
button = self.buttons[i][j]
button.bind('<Enter>', lambda event: self.on_enter(event, button))
def on_enter(self, e, button):
button['background'] = 'green'
需要明確的是,我希望能夠在按住左鍵並同時懸停一個按鈕時更改按鈕的顏色。
謝謝你幫助我。
編輯:刪除了代碼圖片並提供了可以復制和粘貼的內容。
第二次編輯:代碼大約有 100 行,但要點是有一個 tkinter 按鈕的二維數組,我提供的代碼顯示了負責更改按鈕顏色的 2 個函數。 如果需要更多代碼,我會把它放進去。
您可以將根 window 上的<B1-Motion>
綁定到回調。 然后在回調內部,使用.winfo_pointerxy()
獲取鼠標 position 和.winfo_containing()
查找鼠標指針下方的按鈕並更改其背景顏色:
例子:
def on_drag(event):
x, y = root.winfo_pointerxy()
btn = root.winfo_containing(x, y)
if btn:
btn.config(bg="green")
# root is the root window
root.bind('<B1-Motion>', on_drag)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.