将函数从另一个文件读入python中的按钮

时间:2017-02-03 17:54:56

标签: python function button tkinter

我在Python 3.6中创建了一个名为gui_check.py的主代码。

代码如下所示:

from tkinter import *
from urlread import givenumbers

top = Tk()
top.geometry("400x400")

B = Button(top, text = "Hello", command = givenumbers())
B.place(x = 50,y = 50)

top.mainloop()

在这段代码中,有一个名为givenumbers()的函数,它是另一个打印数字的文件(名为urlread.py)的函数。

我想得到的结果是带有按钮的gui,当我点​​击它时,它会调用函数givenumber()。 但是,我得到的结果是,当我运行代码时,即使没有单击按钮,它也会在打开gui时运行givenumber()(打印数字)。

1 个答案:

答案 0 :(得分:2)

删除括号中的括号:

B = Button(top, text = "Hello", command = givenumbers())

所以你应该:

B = Button(top, text = "Hello", command = givenumbers)

代替。

阅读http://effbot.org/zone/tkinter-callbacks.htm