2017-07-08 433 views
0

晚上好!我试图弄清楚如何获得一个按钮,当点击后,打开另一个Gui文件夹中的另一个.py文件。 (我已经尝试了其他问题中给出的每个答案,这些答案可能会远程回答我的问题)。Tkinter 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() 

任何帮助将是惊人的,谢谢!

回答

0

您需要导入其他.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

+0

感谢您的答复!就这样我有一个理解。我会将import emdmenu.py放在导入tkinter下,然后按下我想要按下的按钮,然后调出另一个文件,我只是把command = emdmenu.py,对吧? (目前当我这样做时,它会首先调出emdmenu.py,当我关闭该窗口时,它会弹出一个我想要启动的窗口) – ChrisCrad

+0

不,它应该是'command = function_Name'函数,您的调用应该在函数中形成。导入的任何代码都会在导入时执行,因此请检查您正在导入的文件并确保所有逻辑都位于函数内 – Joe

+0

我强烈建议使用新波士顿。他们有一个网站和YouTube频道。有一整套tkinter系列你可能会觉得有用 – Joe