2017-07-08 7 views
0

Good Evening!私はクリックすると、別の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

は、あなただけの他のモジュールと同様に行い、あなたの現在の1

に、他の.pyファイルをインポートする必要があります。

import myfile.pyは、外部スクリプト内の関数への外部スクリプトに

リンクボタンを別のTkinterの関数を作成します。

ボタンはcommandキーとリンクします。 do_somethingは、他の関数の名前です。この例では

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

:それは次のようになります。ここであなたは実際には()command引数に入れてはいけません。しかしdo_something()はあなたのコードを見てみると、あなたのコメントを読んで、通常のfunciton

EDIT である私は何あなたが欠落していることは関数を作成し、呼び出しの概念だと思います。外部ファイルのコードは、何かをインポートしたときに実行されるため、最初に実行されます。これを避けるには、コードを関数の中に入れます。

あなたはこのような何かが必要ですあなたの外部ファイルで:

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

を次に、あなたのメインのファイルにボタンが持っている必要があります。応答のための

command=func_name

+0

感謝を!ちょうど私には理解があります。私はimport tkinterの下にimport emdmenu.pyを置いて、次に押したいボタンを押して、他のファイルを表示させます。ちょうどcommand = emdmenu.pyと書かれていますか? (現時点では、最初にemdmenu.pyを起動すると、そのウィンドウを閉じると、開始したいウィンドウがポップアップします) – ChrisCrad

+0

いいえ、 'command = function_Name'関数を呼び出す関数は関数でなければなりません形。インポートされたコードはインポート時に実行されるため、インポートするファイルをチェックし、すべてのロジックが関数内にあることを確認してください。 – Joe

+0

私は非常にthenewbostonをお勧めします。彼らはウェブサイトとYouTubeチャンネルを持っています。あなたが有用と感じるかもしれないtkinterにシリーズ全体があります。 – Joe

関連する問題