[英]Tkinter Gui linking button to .py file to open another Gui
晚上好! 我试图弄清楚如何获得一个按钮,当单击该按钮时,会在同一文件夹中的另一个.py文件中打开另一个Gui。 (我尝试了其他问题中给出的所有答案,这些问题可能会为我提供远程答案)。
enter code here
#this file is called main.py
from tkinter import *
root1 = Tk()
root1.title("ProQA-ish")
fphoto = PhotoImage(file="../icon/fireorig.png") #change wd to file named icon
fireButton = Button(root1, image=fphoto)
fireButton.config( height=228, width=200)
mphoto = PhotoImage(file="../icon/ems.png") #change wd to file named icon
emsButton = Button(root1, image=mphoto)
emsButton.config( height=224, width=197)
fireButton.pack(side=LEFT)
emsButton.pack(side=RIGHT)
root1.mainloop()
enter code here
#this is called emdmenu.py
from tkinter import *
root = Tk()
root.title("Emergency Medical Dispatch")
root.iconbitmap(default='../icon/fire.ico')
#----Window------
topframe = Frame(root)
topframe.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
#---Create Buttons for choices----
abdominalPnB = Button(topframe, text="01_Abdominal Pain")
abdominalPnB.config(anchor="w", width=20, height=1)
abdominalPnB.grid(row=0, column=0)
allergyrxB = Button(topframe, text="02_Allergic Reaction")
allergyrxB.config(anchor="w", width=20, height=1)
allergyrxB.grid(row=1, column=0)
#ect..
root.mainloop()
任何帮助都将是惊人的,谢谢!
您需要将该其他.py文件导入到当前文件中
就像其他模块一样执行此操作。 import myfile.py
在外部脚本中创建另一个tkinter函数
将按钮链接到外部脚本中的函数。
该按钮与command
键链接。 因此它应该看起来像:
but = Button(bottom, text='click me', command=do_something)
在此示例中, do_something
是另一个函数的名称。 您实际上没有在command
参数中放入()
很奇怪。 但是do_something()
是正常的功能
编辑查看代码并阅读注释,我认为您缺少的是创建和调用函数的概念。 您的外部文件中的代码将首先执行,因为在您导入某些内容时,该代码已运行。 为避免这种情况,请将代码放入函数中。
在您的外部文件中,您应该具有以下内容:
def func_name():
print('hello world')
然后在您的主文件中,按钮应具有:
command=func_name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.