如题所示
from tkinter import *
root = Tk()
def b1cmd():
print(b1.__dict__)
b1 = Button(root,text = 'b1',)
command = b1cmd()
b1.pack()
def b2cmd():
#.......
#......
root.mainloop()
我们发现,点击按钮1后,触发的b1cmd函数只能返回b1的字典类型和属性,而不能直接返回按钮b1。对此我们可以用event事件对象来返回自身。
代码如下:
from tkinter import *
root = Tk()
def b1cmd():
print(b1.__dict__)
b1 = Button(root,text = 'b1',)
command = b1cmd()
b1.pack()
def b2cmd(event):
print(event.widget['text'])
b2=Button(root,text = 'b2')
b2.pack()
b2.bind("<Button-1>",b2cmd)
root.mainloop()