2016-03-30 155 views
-1

クリックしたラジオボタンの値を取得し、この値を使用する必要があります。Python Tkinter - ラジオボタンで選択を取得

クリックしたラジオボタンの値を取得する方法は何ですか?

セットアップにコードラジオボタンは次のとおりです。 どのボタンが押された追跡するtk.IntVar()を作成します。

radio_uno = Radiobutton(Main,text='Config1', value=1,variable = 1) 
radio_uno.pack(anchor=W,side=TOP,padx=3,pady=3) 
radio_due = Radiobutton(Main,text='Config2', value=2,variable =1) 
radio_due.pack(anchor=W,side=TOP,padx=3,pady=3) 
radio_tre = Radiobutton(Main,text='Config3', value=3,variable = 1) 
radio_tre.pack(anchor=W,side=TOP,padx=3,pady=3) 

答えて

1

は、これは、1つのソリューションです。私はあなたがfrom tkinter import *をしたと仮定しています。そして、あなたが作ることができ

which_button_is_selected = radio_var.get() 

:次にradio_varの値を表示するget()メソッドを使用し

radio_uno = Radiobutton(Main,text='Config1', value=1,variable = radio_var) 
radio_due = Radiobutton(Main,text='Config2', value=2,variable = radio_var) 
radio_tre = Radiobutton(Main,text='Config3', value=3,variable = radio_var) 

radio_var = IntVar() 

あなたは、あなたのボタンを宣言した方法を変更する必要がありますenum、または選択されたボタンに応じて何かを行う句がifの3つだけです。

if(which_button_is_selected == 1): 
    #button1 code 
elif(which_button_is_selected == 2): 
    #button2 code 
else(which_button_is_selected == 3): 
    #button3 code 
+1

これは単なる解決策ではなく、解決策です。 –

関連する問題