Tkinter 5 check/radio button
References
- http://effbot.org/tkinterbook/checkbutton.htm
- http://effbot.org/tkinterbook/radiobutton.htm
- http://bbs.fishc.com/thread-59197-1-1.html
- http://bbs.fishc.com/thread-59198-1-1.html
简介
- Checkbutton, 多选按钮,可以表示两种状态:On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用。on/off的值可以设置,默认是1/0.
- Radiobutton为单选按钮,即在同一组内只能有一个按钮被选中,每当选中组内的一个按钮时,其它的按钮自动改为非选中态,与其他控件不同的是:它有组的概念。同样可以设置回调函数。
Check Button类
-
常用属性介绍
- command(回调函数),
- offvalue(不被选值),
- onvalue(被选值),
- variable(绑定变量),
- text(显示文本)
- textvariable (绑定显示文本的变量)
- background(背景色), bg,
- foreground(前景色), fg,
- font(字体), 用元祖设置
- underline(指定下滑字符),
- padx, pady(填充), borderwidth(边框宽度), bd, width(宽), height(高)
- justify(多行文本对齐方式),
- wraplength(自动换行长度)
- anchor(锚点)
- state(状态)
- cursor (指定鼠标放到widget的样式)
- indicatoron, relief, (设置按钮样式)
- image, bitmap, (设置图像)
- activebackground, activeforeground, disabledforeground, highlightbackground, highlightcolor, highlightthickness, selectcolor, selectimage, takefocus,
-
方法
- deselect() – 取消 Checkbutton 组件的选中状态,也就是设置 variable 为 offvalue。
- select() – 将 Checkbutton 组件设置为选中状态,也就是设置 variable 为 onvalue。
- toggle() – 切换 Checkbutton 组件的状态(选中 -> 未选中 / 未选中 -> 选中)。
- flash() – 刷新 Checkbutton 组件,该方法将重绘 Checkbutton 组件若干次(在 ACTIVE 和 NORMAL 状态间切换)。
- invoke() – 调用 Checkbutton 中 command 选项指定的函数或方法,并返回函数的返回值。 – 如果 Checkbutton 的状态是 DISABLED(不可用)或没有指定 command 选项,则该方法无效。
-
代码实例
def _gui_int(self): # check button 1 info_label_1 = tk.Label(self.window, text='Choose your Script Language:', bg='green', fg='red') info_label_1.pack(anchor='w') self.check_var = [] for lang in ['Python', 'Ruby', 'Perl', 'Lua']: self.check_var.append(tk.IntVar()) tk.Checkbutton(self.window, text=lang, variable=self.check_var[-1], command=self.check_call).pack(anchor='w', padx=20) self.text_1 = tk.Text(self.window, height=2) self.text_1.pack(anchor='w') # check button 2 tk.Label(self.window, text='Choose your Compiling Language:', bg='yellow', fg='red').pack(anchor='w') check_item = ['C', 'C++', 'C#', 'Java'] self.check_dict = dict().fromkeys(check_item) for lang in check_item: self.check_dict[lang] = tk.BooleanVar() tk.Checkbutton(self.window, text=lang, variable=self.check_dict[lang], onvalue='True', offvalue='False').pack(anchor='w', padx=20) tk.Button(self.window, text="show", command=self.show_choice).pack(anchor='w') self.text_2 = tk.Text(self.window, height=2) self.text_2.pack() def show_choice(self): self.text_2.delete(0.0, tk.END) bool_lst = [x.get() for x in self.check_dict.values()] # 获取选择的内容,方法1 bool_lst_1 = np.array(bool_lst) choose_lst_1 = np.array(list(self.check_dict.keys())) info = 'you have chose:' + str(choose_lst_1[bool_lst_1]) # 获取选择的内容,方法2 # option_lst = self.check_dict.keys() # choose_lst_2 = [x for x, y in zip(option_lst, bool_lst) if y==True] # info = 'you have chose:' + str(choose_lst_2) self.text_2.insert(0.0, info) def check_call(self): self.text_1.delete(0.0, tk.END) info = [x.get() for x in self.check_var] self.text_1.insert(0.0, str(info))
Radio Button类
- 常用属性介绍
- 跟CheckButton相比,至少了onvalue 和 offvalue 两个属性,其他全部一样
- radioButton,只要部件的
variable
属性设置的是同一个变量,那么他们就是一组,即一组只能单选
-
代码实例
def _gui_int(self): # radio button tk.Label(self.window, text='Choose your favorite country:', bg='green', fg='red').pack(anchor='w') self.radio_lst = ['China', 'USA', 'Canada', 'UK'] self.radio_var = tk.IntVar() self.radio_var.set(1) for index in range(len(self.radio_lst)): tk.Radiobutton(self.window, text=self.radio_lst[index], variable=self.radio_var, value=index, command=self.radio_call).pack(anchor='w') # for index in range(len(self.radio_lst)): # tk.Radiobutton(self.window, # text=self.radio_lst[index], # variable=self.radio_var, # value=index, # indicatoron=False).pack(anchor='w') self.text_var = tk.StringVar() self.text_var.set(self.radio_lst[self.radio_var.get()]) show_frame = tk.Frame(self.window) show_frame.pack(anchor='center') tk.Label(show_frame, textvariable=self.radio_var, bg='red', fg='white').grid(row=0, column=0) tk.Label(show_frame, textvariable=self.text_var, bg='black', fg='white').grid(row=0, column=1) def radio_call(self): self.text_var.set(self.radio_lst[self.radio_var.get()])