簡體   English   中英

Tkinter Gui鏈接按鈕到.py文件以打開另一個Gui

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM