简体   繁体   English

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

[英]Tkinter Gui linking button to .py file to open another Gui

Good Evening! 晚上好! I am trying to figure out how to get a button to, when clicked, open up another Gui in another .py file that is in the same file folder. 我试图弄清楚如何获得一个按钮,当单击该按钮时,会在同一文件夹中的另一个.py文件中打开另一个Gui。 (I have tried every answer given in other questions that remotely might answer this for me). (我尝试了其他问题中给出的所有答案,这些问题可能会为我提供远程答案)。

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

Any help would be amazing, thank you! 任何帮助都将是惊人的,谢谢!

You need to import that other .py file into your current one 您需要将该其他.py文件导入到当前文件中

Do this just like any other module. 就像其他模块一样执行此操作。 import myfile.py

create another tkinter function in the external script 在外部脚本中创建另一个tkinter函数

link the button to the function in the external script. 将按钮链接到外部脚本中的函数。

The button links with the command key. 该按钮与command键链接。 so it should look like: 因此它应该看起来像:

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

In this example do_something is the name of the other function. 在此示例中, do_something是另一个函数的名称。 It is weird here you dont actually put () into the command argument. 您实际上没有在command参数中放入()很奇怪。 But do_something() is a normal funciton 但是do_something()是正常的功能

EDIT Looking at your code and reading your comments I think that what your missing is the concept of creating and calling functions. 编辑查看代码并阅读注释,我认为您缺少的是创建和调用函数的概念。 Your code in the external file is executing first because when you import something it is ran. 您的外部文件中的代码将首先执行,因为在您导入某些内容时,该代码已运行。 To avoid this put the code inside a funtion. 为避免这种情况,请将代码放入函数中。

In your external file you should have something like this: 在您的外部文件中,您应该具有以下内容:

def func_name():
    print('hello world')

Then in your main file the button should have: 然后在您的主文件中,按钮应具有:

command=func_name

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

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