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()