简体   繁体   English

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

[英]reading function from another file into a button in python

I'm making a main code in Python 3.6 called gui_check.py.我在 Python 3.6 中编写了一个名为 gui_check.py 的主要代码。

The code looks like this:代码如下所示:

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()

In this code, there is a function called givenumbers() which is a function from another file (called urlread.py) that prints numbers.在这段代码中,有一个名为givenumbers()的函数,它是另一个文件(名为 urlread.py)中的一个函数,用于打印数字。

The result I wanted to get is a gui with a button that when I click on it, it calls the function givenumber() .我想要得到的结果是一个带有按钮的 gui,当我单击它时,它会调用函数givenumber() However, the result I get is that when I run the code it runs givenumber() (print the numbers) while opening the gui even without me clicking on the button.但是,我得到的结果是,当我运行代码时,即使我没有单击按钮,它也会在打开 gui 时运行givenumber() (打印数字)。

Remove the parentheses in:删除括号:

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

So you should have:所以你应该有:

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

instead.反而。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM