我一直在浏览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()
答案 0 :(得分:1)
Tkinter radiobuttons需要两个选项才能正常运行,而您未提供:variable
(用于存储组中所选按钮值的Tk变量,通常为IntVar或StringVar)和{{ 1}}(组中每个按钮的一个不同值,当选择该按钮时存储在变量中)。所以你需要:
value
在顶部某处,然后是:
self.selectedTown = tkinter.StringVar()
self.selectedTown.set('Town')
每个按钮;
之类的东西ttk.Radiobutton(self, text='Town', value='Town', variable=self.selectedTown)
当您想要检查选择了哪个项目时。
PS:将所有按钮分配给同一个变量print self.selectedTown.get()
是没有意义的。无论如何,您无法自行分配实际按钮:您每次都会分配无,这是每行self.rad_button
调用的结果。