2016-05-01 20 views
0

tkinterを使用してムービーの推奨GUIを作成しています。ユーザーがラジオボタンを選択すると、ラジオボタンが選択された新しいウィンドウで開きます。だから誰かがコメディーを選んだら、私は別の映画でコーディングして、新しいウィンドウでランダムにポップアップさせることができます。私はウィンドウを開くためのコマンドを用意していますが、選択した選択肢を呼び戻すのに問題があります。新しいウィンドウでのラジオボタンの選択

from tkinter import * 

class movie1: 
def __init__(self, master): 
    self.master = master 
    master.title("Movie Recommendation") 

    self.label = Label(master, text= "Welcome to the movie recommendation application! \n Please select the genre of the movie you would like to see.") 
    self.label.pack(padx=25, pady=25) 

    CheckVar1 = StringVar() 

    self.radiobutton = Radiobutton(master, text = "Action", variable=CheckVar1, value=1, command=self.reco) 
    self.radiobutton.pack(side=TOP, padx=10, pady=10) 

    self.radiobutton = Radiobutton(master, text = "Comedy", variable=CheckVar1, value=2, command=self.reco) 
    self.radiobutton.pack(side=TOP, padx=10, pady=10) 

    self.radiobutton = Radiobutton(master, text = "Documentary", variable=CheckVar1, value=3, command=self.reco) 
    self.radiobutton.pack(side=TOP, padx=10, pady=10) 

    self.radiobutton = Radiobutton(master, text = "Horror", variable=CheckVar1, value=4, command=self.reco) 
    self.radiobutton.pack(side=TOP, padx=10, pady=10) 

    self.radiobutton = Radiobutton(master, text = "Romance", variable=CheckVar1, value=5, command=self.reco) 
    self.radiobutton.pack(side=TOP, padx=10, pady=10) 

    self.radiobutton.cget("value") 


def reco(self): 
    self.newWindow = Toplevel(self.master) 
    if ("value") == 1: 
     print("1") 


root = Tk() 
my_gui = movie1(root) 
root.mainloop() 
+0

Radiobuttonを確認した例http://www.tutorialsp.com/jp/python/tk_radiobutton.htm –

答えて

0

これはあなたを助けることがあります。

最初に:回答の値がradiobuttonであるため、11行目をself.CheckVar1 = IntVar()に変更してください。文字列ではなく整数です。さらに、メソッドの後半でアクセスしたいので、CheckVar1変数にself.を追加します。

第2行:33行目をif self.CheckVar1.get() == 1:に変更します。ここでは、変数self.CheckVar1に関連付けられた.get()メソッドを使用して、値を取得しました。

Actionオプションを選択すると、スクリプトを実行すると、IDLEで"1"が印刷されます。

関連する問題