Tkinter Gui链接按钮到.py文件以打开另一个Gui

2024-10-01 09:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

晚上好!我试图弄清楚如何让一个按钮在单击时在同一个文件夹中的另一个.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()

谢谢你的帮助!在


Tags: pyconfigbuttonrootwidthsidepackfile
1条回答
网友
1楼 · 发布于 2024-10-01 09:22:07

您需要将另一个.py文件导入到当前文件中

像其他模块一样这样做。import myfile.py

在外部脚本中创建另一个tkinter函数

将按钮链接到外部脚本中的函数。在

按钮与command键链接。所以它应该看起来像:

but = Button(bottom, text='click me', command=do_something)

在本例中,do_something是另一个函数的名称。奇怪的是你没有把()放入command参数中。但是do_something()是一个正常函数

编辑 看看你的代码和评论,我认为你缺少的是创建和调用函数的概念。外部文件中的代码是首先执行的,因为当您导入某些内容时,它是运行的。为了避免这种情况,请将代码放入函数中。在

在外部文件中,您应该具有以下内容:

^{pr2}$

然后在主文件中,按钮应该具有:

command=func_name

相关问题 更多 >