悬停时,Tkinter绑定按钮工作不正常



为什么我的Tkinter绑定按钮工作不正确?当我用鼠标点击开始按钮时,大约按钮会改变!显示图像

这是我的代码:

import tkinter as tk

class Panel(tk.Tk):
def __init__(self):
super().__init__()
# window config
self.title('test')
self.geometry('300x300')
self.configure(bg='#2b2e37')


def btns(self, x, y, text, bg, fg):
def on_enter(ctx):
self.btn['background'] = fg
self.btn['foreground'] = bg
def on_leave(ctx):
self.btn['background'] = bg
self.btn['foreground'] = fg

self.btn = tk.Button(self, text=text, bg=bg, fg=fg, activebackground=fg, activeforeground=bg, border=0, width=42, height=2)
self.btn.bind('<Enter>', on_enter)
self.btn.bind('<Leave>', on_leave)
self.btn.place(x=x, y=y)

if __name__ == '__main__':
window = Panel()
window.btns(0, 190, 'S T A R T', '#2b2e37', '#56fc03')
window.btns(0, 230, 'A B O U T', '#2b2e37', '#f5ec42')
window.mainloop()

我认为这是为了我的oop代码,我写得很简单,工作也很正确。抱歉我的英语不好

发生事件的按钮会传递给widget属性中ctx参数中的事件处理程序,因此您可以使用它来代替self.btn:

def btns(self, x, y, text, bg, fg):
def on_enter(ctx):
ctx.widget['background'] = fg
ctx.widget['foreground'] = bg
def on_leave(ctx):
ctx.widget['background'] = bg
ctx.widget['foreground'] = fg

在这些事件处理程序中使用self.btn代替ctx.widget的问题是,第二次调用btns时,self.btn会被第二个按钮替换,因此添加到第一个按钮的事件处理程序最终会更改第二个按键。

我通过分别为每个按钮编写代码来修复代码。

新代码:

def btns(self):
def on_enter1(ctx):
self.btn1['background'] = '#56fc03'
self.btn1['foreground'] = '#2b2e37'
def on_leave1(ctx):
self.btn1['background'] = '#2b2e37'
self.btn1['foreground'] = '#56fc03'
def on_enter2(ctx):
self.btn2['background'] = '#f5ec42'
self.btn2['foreground'] = '#2b2e37'
def on_leave2(ctx):
self.btn2['background'] = '#2b2e37'
self.btn2['foreground'] = '#f5ec42'

self.btn1 = tk.Button(self, text='S T A R T', bg='#2b2e37', fg='#56fc03', activebackground='#56fc03', activeforeground='#2b2e37', border=0, width=42, height=2)
self.btn2 = tk.Button(self, text='A B O U T', bg='#2b2e37', fg='#f5ec42', activebackground='#f5ec42', activeforeground='#2b2e37', border=0, width=42, height=2)
self.btn1.bind('<Enter>', on_enter1)
self.btn1.bind('<Leave>', on_leave1)
self.btn2.bind('<Enter>', on_enter2)
self.btn2.bind('<Leave>', on_leave2)
self.btn1.place(x=0, y=190)
self.btn2.place(x=0, y=230)
if __name__ == '__main__':
window = Panel()
window.label()
window.btns()
window.mainloop()

最新更新