Python中Tkinter按钮的更改命令方法

2024-04-19 09:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个新的Button对象,但在创建时没有指定command选项。Tkinter中是否有方法在创建对象后更改命令(onclick)函数?


Tags: 对象方法函数命令tkinter选项buttoncommand
1条回答
网友
1楼 · 发布于 2024-04-19 09:38:58

虽然Eli Courtwright's程序可以很好地工作,但您真正想要的似乎只是在实例化任何属性后重新配置的一种方法,您可以在实例化时设置这些属性。方法是configure()方法。

from Tkinter import Tk, Button

def 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]按钮,那么你也必须实现(复制现有代码)按键事件。通过.configure设置command选项要容易得多。

实例化后唯一不能更改的属性是name

相关问题 更多 >