2017-03-16 74 views
0

我一直在經歷Tkinter的例子,現在正在使用按鈕。 當我選擇或取消選擇一個按鈕時,所有其他按鈕都執行相同的操作。如何在一次只更換一個按鈕的情況下更改它?我試過其他的東西,但我沒有任何運氣。如何在Python Tkinter中一次只選擇一個單選按鈕?

這裏是我當前的代碼:

import tkinter 
from tkinter import ttk 


class Adder(ttk.Frame): 
    """The adders gui and functions.""" 
    def __init__(self, parent, *args, **kwargs): 
     ttk.Frame.__init__(self, parent, *args, **kwargs) 
     self.root = parent 
     self.init_gui() 

    def init_gui(self): 
     """Builds GUI.""" 
     self.root.title('IDL - I.D. Lookup') 

     self.grid(column=0, row=0, sticky='nsew') # this starts the entire form 

     self.num1_entry = ttk.Entry(self, width=15) # width of first input box 
     self.num1_entry.grid(sticky='W', column=1, row = 2) # column and row it is placed on # sticky='w' justifies or aligns to left 

     self.num2_entry = ttk.Entry(self, width=10) # width of second input box 
     self.num2_entry.grid(sticky='W', column=1, row=3) # column and row it is placed on 

     self.calc_button = ttk.Button(self, text='Calculate') # button 
     self.calc_button.grid(column=0, row=4, columnspan=4) # column and row it is placed on 

     self.answer_frame = ttk.LabelFrame(self, text='Answer', height=100) # answer box 
     self.answer_frame.grid(column=0, row=5, columnspan=4, sticky='nesw') 

     self.rad_button = ttk.Radiobutton(self, text='Town').grid(sticky='W', column=0,row=6, columnspan=1) # sticky W to align everything to left   
     self.rad_button = ttk.Radiobutton(self, text='Town1').grid(sticky='W', column=0,row=7, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town2').grid(sticky='W', column=0,row=8, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town3').grid(sticky='W', column=0,row=9, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town4').grid(sticky='W', column=0,row=10, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town5').grid(sticky='W', column=0,row=11, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town6').grid(sticky='W', column=0,row=12, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town7').grid(sticky='W', column=0,row=13, columnspan=1) 
     self.rad_button = ttk.Radiobutton(self, text='Town8').grid(sticky='W', column=0,row=14, columnspan=1) 

     self.answer_label = ttk.Label(self.answer_frame, text='') # text=' ' holds text in the answer box 
     self.answer_label.grid(column=0, row=0) 

     # Labels that remain constant throughout execution. 
     ttk.Label(self, text='IDL - I.D. Lookup').grid(column=0, row=0, 
       columnspan=4) 
     ttk.Label(self, text='Name').grid(column=0, row=2, 
       sticky='w') 
     ttk.Label(self, text='I.D.').grid(column=0, row=3, 
       sticky='w') 

     ttk.Separator(self, orient='horizontal').grid(column=0, # line under title. 
       row=1, columnspan=4, sticky='ew') 

     for child in self.winfo_children(): 
      child.grid_configure(padx=10, pady=4) # padx 10 adds horizontal padding on the out edge of window 



if __name__ == '__main__': 
    root = tkinter.Tk() 
    Adder(root) 
    root.mainloop() 

回答

1

Tkinter的單選按鈕需要的正常運行兩個選擇,你沒有提供:variable(一Tk的變量來選擇按鈕的值存儲在一組,通常一個IntVar或StringVar)和value(該組中每個按鈕的不同值,當該按鈕被選中時將被存儲在該變量中)。所以,你需要:

self.selectedTown = tkinter.StringVar() 
self.selectedTown.set('Town') 

地方在頂部,那麼這類話:

ttk.Radiobutton(self, text='Town', value='Town', variable=self.selectedTown) 

每個按鈕;然後像

print self.selectedTown.get() 

當你想檢查選擇哪個項目。 PS:將所有按鈕分配給同一個變量self.rad_button是沒有意義的。無論如何,您並未分配實際的按鈕本身:您每次都分配「無」,這是每條線路上的.grid()呼叫的結果。

相關問題