2016-08-23 21 views
0

おかげさまで、私はシンプルなGUIを書いて、ボタン「リンゴジュース」をクリックして別のGUIを開きたいと思っています。しかし、 "apple juice" GUIが起動時に開きます。Python 3 GUIボタンをクリックして別のGUIを開く - 起動しない

コードに間違った文字を入力しましたか?そうでない場合は、ボタンをクリックしたときにのみどうすれば開くことができますか?ここに私のコード:

import Tkinter 

############### 

win = Tkinter.Tk() 
win.geometry("500x25") 

Tkinter.Label(win, text="You've chosen apple juice!", font="bold").pack() 

############## 

root = Tkinter.Tk() 
root.geometry("500x300") 

# Label asking what drink 

Tkinter.Label(root, text="What drink would you like?", bg="goldenrod", font="bold").pack() 

# white space 

Tkinter.Label(root, text="").pack() 

#Choices 

Tkinter.Label(root, text="Whichever choice you want, simply press the buton!").pack() 

# Apple juice button 

def apple_juice(): 
    win.mainloop() 

Tkinter.Button(root, text="Apple Juice", bg="SkyBlue1", command=apple_juice).pack() 

root.mainloop() 
+0

は誰がどのようにコードの入力ブロック全体ではなく、個々のものに知っている場合、それをいただければ幸いです。 –

答えて

2

あなたの問題はいくつかの誤解から来ています。

ハイランダーのように、Tkinterプログラムにはメインループが1つしかない場合があります。だから、他のルートウィンドウを作成しているときは、それが表示されます。期間。また、ウィンドウの表示を開始するのは.mainloop()ではありません。イベントの処理を開始するだけです。

最後に、恐ろしいことをしない限り、import Tkinterは失敗するため、このコードはPython3ではありません。

ここでは、実際にPython2 両方のpython3で動作しますいくつかのコードです:余談として

try: 
    import Tkinter as tk 
    import tkMessageBox as mb 
except ImportError: 
    import tkinter as tk 
    import tkinter.messagebox as mb 
############## 

root = tk.Tk() 
root.geometry("500x300") 

############### 

# Label asking what drink 

tk.Label(root, text="What drink would you like?", bg="goldenrod", font="bold").pack() 

# white space 

tk.Label(root, text="").pack() 

#Choices 

tk.Label(root, text="Whichever choice you want, simply press the buton!").pack() 

# Apple juice button 

def apple_juice(): 
    mb.showinfo('showinfo', "You've chosen apple juice!") 

tk.Button(root, text="Apple Juice", bg="SkyBlue1", command=apple_juice).pack() 

root.mainloop() 
+0

ありがとう、明日これを試してみます。また、私は最近、Python 2と3がインストールされているUbuntu 16.04をインストールしました。これがPython 2である可能性はありますか?私は、Python 3のみを持つ方法ではなく、両方を使う方法がないと推測しています。 tryと、ImportErrorコマンドを除いて何を意味するのかを説明することはできますか?私はPythonをかなり新しくしています。ありがとう! –

+0

'Tkinter'はPython 3では' tkinter'に名前が変更されました。 Ubuntuでは 'python3

関連する問題