在Python中更改Tkinter Button的命令方法

在Python中更改Tkinter Button的命令方法,第1张

在Python中更改Tkinter Button的命令方法

尽管Eli Courtwright的程序可以正常运行¹,但是您似乎真正想要的只是在实例化之后重新配置实例化时可以设置的任何属性的一种方法²。这样做是通过configure()方法实现的。

from Tkinter import Tk, Buttondef goodbye_world():    print "Goodbye World!nWait, I changed my mind!"    button.configure(text = "Hello World!", command=hello_world)def hello_world():    print "Hello World!nWait, I changed my mind!"    button.configure(text = "Goodbye World!", command=goodbye_world)root = Tk()button = Button(root, text="Hello World!", command=hello_world)button.pack()root.mainloop()

¹如果只使用鼠标,则为“精细”;如果您关心使用制表符并使用按钮上的[Space]或[Enter],那么您也将必须实现(复制现有代码)按键事件。

command
通过设置选项
.configure
要容易得多。

²实例化后唯一不变的属性是

name



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5667082.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存