在Python中,如何为一段代码分配Tkinter按钮?

时间:2022-09-23 01:06:27

I cannot work out the code for this and I was wondering if any of you do. I want to assign a button in a tkinter menu to a defined function in another piece of Python code (Or to have some way of linking them). I have tried numerous different websites but I cannot find it easily.

我无法解决这个问题的代码,我想知道你是否有人这样做。我想将tkinter菜单中的一个按钮分配给另一段Python代码中的已定义函数(或者有一些链接它们的方法)。我尝试了很多不同的网站,但我找不到它。

websites I have tried:

我试过的网站:

-https://docs.python.org/3/library/tkinter.ttk.html

-www.tutorialspoint.com/python/tk_button.htm

1 个解决方案

#1


This is the first result I get in google:

这是我在谷歌获得的第一个结果:

from Tkinter import *

master = Tk()

def callback():
    print "click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

As you can see, command is used to assign a callback function to run when the user clicks the button.

如您所见,命令用于指定在用户单击按钮时运行的回调函数。

#1


This is the first result I get in google:

这是我在谷歌获得的第一个结果:

from Tkinter import *

master = Tk()

def callback():
    print "click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

As you can see, command is used to assign a callback function to run when the user clicks the button.

如您所见,命令用于指定在用户单击按钮时运行的回调函数。