我有一个基于Tkinter的GUI和一系列按钮。我希望其中一个按钮在按下时从另一个脚本执行命令-testExecute.py
(下面包含两个脚本的代码)。你知道吗
现在,当我启动GUI时,外部脚本函数似乎是在import
上执行,而不是在我按下按钮时执行(按下按钮似乎也不会执行该函数)。我做了一些研究,并在testExecute.py
中包含了if __name__ == "__main__":
位,但它仍然在主脚本的导入时执行。有什么想法吗?你知道吗
回答下面的问题:如果我想把一个参数传递给函数,我该怎么办?因为如果我包含参数,函数会在导入时再次执行。但如果我不包括参数,我会在按下按钮时出错。你知道吗
主脚本:
from tkinter import *
from tkinter.ttk import *
import testExecute as testEx
class mainGUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
def initUI(self):
self.parent.title("GUIV0.1")
self.pack(fill=BOTH, expand=True)
self.columnconfigure(1, weight = 1)
self.columnconfigure(3, pad = 7)
self.rowconfigure(3, weight = 1)
self.rowconfigure(5, pad = 7)
lbl = Label(self, text = "Windows")
lbl.grid(sticky = W, pady=4, padx=5)
area = Text(self)
area.grid(row=1, column=0, columnspan=2, rowspan=4, padx=5, sticky=E+W+S+N)
abtn = Button(self, text="Activate", command = testEx.testFunc())
abtn.grid(row=1, column=3)
cbtn = Button(self, text="Close")
cbtn.grid(row=2, column=3, pady=4)
hbtn = Button(self, text="Help")
hbtn.grid(row=5, column=0, padx=5)
obtn = Button(self, text="OK")
obtn.grid(row=5, column=3)
def main():
root = Tk()
app = mainGUI(root)
root.mainloop()
if __name__ == '__main__':
main()
你知道吗测试执行.py地址:
def testFunc():
print("Test test test")
print("I do nothing, if you see this text, I am hiding in your code!")
if __name__ == "__main__":
testFunc()
创建按钮时,直接执行函数。相反,您应该绑定到函数本身。所以呢
应该是
查看http://effbot.org/tkinterbook/button.htm。你知道吗
问题是,在初始化过程中,调用命令回调。改变
至
你们应该都很好。(注意testFunc后面缺少括号)。你知道吗
相关问题 更多 >
编程相关推荐