Python --- (three) Tkinter widgets: Checkbutton

Previous: Python - (two) Tkinter widgets: Button


                                                The Tkinter Checkbutton Widget

## Introduction

Checkbutton (checkboxes) assembly for enabling determining whether the selected button. Checkbutton assembly may contain text or images, you can be a Python function or method associated therewith, when the button is pressed, the corresponding function or method is performed automatically.

Checkbutton component can only display a single font text, but the text can span multiple lines. Additionally, you can add one of the individual characters underscore (for example, used to represent the keyboard shortcut). By default, tab key is used to switch between the button.

When to use Checkbutton ## components?
Checkbutton assembly is used when you want expression "multi-select more" option may be used in combination as a series of Checkbutton choose one button (usually select "ON" or "OFF" state).

But the deal, "one of many" problems, or to Radiobutton and Listbox components to achieve it.

Usage ##
(refer to Button assembly usage)

Use Checkbutton, you must create a Tkinter variable is used to store the state of the button:

from tkinter import *
 
master = Tk()
 
var = IntVar()
 
c = Checkbutton(master, text="近视的脚踏实地", variable=var)
c.pack()
 
mainloop()

Here Insert Picture Description
By default, variable option is set to 1 indicates the selected state, otherwise set to 0. You can use the option to modify offvalue onvalue and their values, for example, the code below, is set to be long var "T" that is selected, is set to "F" is the opposite:

from tkinter import *
 
master = Tk()
 

var = StringVar()
var.set("T")
c = Checkbutton(master, text="你喜欢Python吗", variable=var, onvalue="T", offvalue="F")
c.pack()
 
mainloop()

Here Insert Picture Description
If you need to track and Checkbutton variable option component object, you may wish to try to combine the two (var below to create a new variable in CheckButton component object, the stored value of v variable):

from tkinter import *
 
master = Tk()
 
v = IntVar()
c = Checkbutton(master, text="加特技", variable=v)
c.var = v
c.pack()
 
mainloop()

Here Insert Picture Description

If your Tkinter code is placed in the class (the actual programming you should do it), then the value of the variable storage options as properties may be a better choice:

class A:
    
    def __init__(self, master):
        self.var = IntVar()
        c = Checkbutton(master, text="DUANG~", variable=self.var, command=self.cb)
        c.pack()
     
    def cb(self):
        print("variable is", self.var.get())
    
a=A(master)

            Here Insert Picture Description            Here Insert Picture Description            Here Insert Picture Description

##parameter

Checkbutton(master=None, **options) (class)

master - parent component

** options - component options, the table below details the specific meaning and usage of each option:

Options meaning
activebackground 1. When the background color is provided Checkbutton active state (a state by state option) of
2. The default value is specified by the system
activeforeground 1. When Checkbutton active set (set state by state option) foreground
2. The default value is specified by the system
anchor 1. Control of text (or image) in the position shown in Checkbutton
2. "n", "ne" , "e", "se", "s", "sw", "w", "nw", or " center "to locate (ewsn on behalf of East and West, north-west left and right on the east)
3. the default value is" center "
background 1. Set the background color
2. The default value is specified by the system
bg Like background
bitmap 1. Specify the bitmap displayed on Checkbutton
2. If the image option is specified, the option is ignored
borderwidth 1. 指定 Checkbutton 的边框宽度
2. 默认值由系统指定,通常是 1 或 2 像素
bd 跟 borderwidth 一样
command 1. 指定于该按钮相关联的函数或方法
2. 当按钮被按下时由 Tkinter 自动调用对应的函数或方法
3. 如果不设置此选项,那么该按钮被按下后啥事儿也不会发生
compound 1. 控制 Checkbutton 中文本和图像的混合模式
2. 默认情况下,如果有指定位图或图片,则不显示文本
3. 如果该选项设置为 "center",文本显示在图像上(文本重叠图像)
4. 如果该选项设置为 "bottom","left","right" 或 "top",那么图像显示在文本的旁边(如 "bottom",则图像在文本的下方)
5. 默认值是 NONE
cursor 1. 指定当鼠标在 Checkbutton 上飘过的时候的鼠标样式
2. 默认值由系统指定
disabledforeground 1. 指定当 Checkbutton 不可用的时候前景色的颜色
2. 默认值由系统指定
font 1. 指定 Checkbutton 中文本的字体
2. 一个 Checkbutton 只能设置一种字体
3. 默认值由系统指定
foreground 1. 设置 Checkbutton 的文本和位图的颜色
2. 默认值由系统指定
fg 跟 foreground 一样
height 1. 设置 Checkbutton 的高度
2. 如果 Checkbutton 显示的是文本,那么单位是文本单元
3. 如果 Checkbutton 显示的是图像,那么单位是像素(或屏幕单元)
4. 如果设置为 0 或者干脆不设置,那么会自动根据 Checkbutton 的内容计算出高度
highlightbackground 1. 指定当 Checkbutton 没有获得焦点的时候高亮边框的颜色
2. 默认值由系统指定,通常是标准背景颜色
highlightcolor 1. 指定当 Checkbutton 获得焦点的时候高亮边框的颜色
2. 默认值由系统指定
highlightthickness 1. 指定高亮边框的宽度
2. 默认值是 1
image 1. 指定 Checkbutton 显示的图片
2. 该值应该是 PhotoImage,BitmapImage,或者能兼容的对象
3. 该选项优先于 text 和 bitmap 选项
indicatoron 1. 指定前边作为选择的小方块是否绘制
2. 默认是绘制的
3. 该选项会影响到按钮的样式,如果设置为 False,则点击后该按钮变成 "sunken"(凹陷),再次点击变为 "raised"(凸起)
justify 1. 定义如何对齐多行文本
2. 使用 "left","right" 或 "center"
3. 注意,文本的位置取决于 anchor 选项
4. 默认值是 "center"
offvalue 1. 默认情况下,variable 选项设置为 1 表示选中状态,反之设置为 0
2. 设置 offvalue 的值可以自定义未选中状态的值(详见上方用法举例)
onvalue 1. 默认情况下,variable 选项设置为 1 表示选中状态,反之设置为 0
2. 设置 onvalue 的值可以自定义选中状态的值(详见上方用法举例)
padx 1. 指定 Checkbutton 水平方向上的额外间距(内容和边框间)
2. 默认值是 1
pady 1. 指定 Checkbutton 垂直方向上的额外间距(内容和边框间)
2. 默认值是 1
relief 1. 指定边框样式
2. 该值通常是 "flat",除非你设置 indicatoron 选项为 False
3. 如果 indicatoron 为 False,你还可以设置 "sunken","raised","groove" 或 "ridge"
selectcolor 1. 选择框的颜色(就是打勾勾的那个正方形小框框)
2. 默认值由系统指定
selectimage 1. 设置当 Checkbutton 为选中状态的时候显示的图片
2. 如果没有指定 image 选项,该选项被忽略
state 1. 指定 Checkbutton 的状态
2. 默认值是 "normal"
3. 另外你还可以设置 "active" 或 "disabled"
takefocus 1. 如果是 True,该组件接受输入焦点(用户可以通过 tab 键将焦点转移上来)
2. 默认值是 False
text 1. 指定 Checkbutton 显示的文本
2. 文本可以包含换行符
3. 如果设置了 bitmap 或 image 选项,该选项则被忽略
textvariable 1. Checkbutton 显示 Tkinter 变量(通常是一个 StringVar 变量)的内容
2. 如果变量被修改,Checkbutton 的文本会自动更新
underline 1. 跟 text 选项一起使用,用于指定哪一个字符画下划线(例如用于表示键盘快捷键)
2. 默认值是 -1
3. 例如设置为 1,则说明在 Checkbutton 的第 2 个字符处画下划线
variable 1. 将 Checkbutton 跟一个 Tkinter 变量关联
2. 当按钮按下时,该变量在 onvalue 和 offvalue 之间切换
3. 这个切换的过程是完全自动的
width 1. 设置 Checkbutton 的宽度
2. 如果 Checkbutton 显示的是文本,那么单位是文本单元
3. 如果 Checkbutton 显示的是图像,那么单位是像素(或屏幕单元)
4. 如果设置为 0 或者干脆不设置,那么会自动根据 Checkbutton 的内容计算出宽度
wraplength 1. 决定 Checkbutton 的文本应该被分成多少行
2. 该选项指定每行的长度,单位是屏幕单元
3. 默认值是 0

##方法
deselect()

— 取消 Checkbutton 组件的选中状态,也就是设置 variable 为 offvalue。

flash()

— 刷新 Checkbutton 组件,该方法将重绘 Checkbutton 组件若干次(在"active" 和 “normal” 状态间切换)。

invoke()

— 调用 Checkbutton 中 command 选项指定的函数或方法,并返回函数的返回值。

— 如果 Checkbutton 的state(状态)"disabled"是 (不可用)或没有指定 command 选项,则该方法无效。

select()

— 将 Checkbutton 组件设置为选中状态,也就是设置 variable 为 onvalue。

toggle()

— 切换 Checkbutton 组件的状态(选中 -> 未选中 / 未选中 -> 选中)。


下一篇:Python —(四)Tkinter窗口组件:RadioButton

Published 247 original articles · won praise 116 · views 280 000 +

Guess you like

Origin blog.csdn.net/w15977858408/article/details/104152034