首页 > 解决方案 > Tkinter .bind 无法绑定使用函数创建的按钮

问题描述

基本上,我想使用函数创建一个具有 .bind() 的按钮

def button():
   btn = Button(main, text="HI").pack()
   btn.bind("<button-1>", lambda x="hi":print(x))
main = Tk()
button()
main.mainloop()

但是它没有工作,而是返回

Traceback (most recent call last):
File "d:\source\Learn_management\test.py", line 9, in <module>
button()
File "d:\source\Learn_management\test.py", line 6, in button
btn.bind("<Shift_L>", lambda x="hi":print(x))
AttributeError: 'NoneType' object has no attribute 'bind'

有没有快速解决这个问题的方法?感谢阅读

标签: pythontkinter

解决方案


推荐阅读