YouTip LogoYouTip

Python Tk Checkbutton

# Python Tkinter Checkbutton [![Image 1: Python GUI Programming](#) Python GUI Programming](#) Python Tkinter Checkbutton is used to select options we need. It has a small square box in front of it; if selected, it shows a checkmark. Clicking it again removes the checkmark to deselect it. ### Syntax The syntax format is as follows: w = Checkbutton ( master, option=value, ... ) * **master**: The parent container of the button. * **options**: Optional parameters, which are the settable properties of the button. These options can be set in the form of key=value and separated by commas. | No. | Option & Description | | --- | --- | | 1 | **activebackground** Background color when the mouse is over the button. | | 2 | **activeforeground** Foreground color when the mouse is over the button. | | 3 | **bg** Background color of the button. | | 4 | **bitmap** Bitmap image. | | 5 | **bd** Border size, default is 2 pixels. | | 6 | **command** Associated function; this function is executed when the button is clicked. | | 7 | **cursor** Cursor shape setting, such as arrow, circle, cross, plus, etc. | | 8 | **disabledforeground** Foreground color for disabled options. | | 9 | **font** Text font. | | 10 | **fg** Foreground color of the option. | | 11 | **height** Number of text lines in the checkbutton, default is 1. | | 12 | **highlightcolor** Highlight color when focused. | | 13 | **image** Whether to use an icon. | | 14 | **justify** When displaying multi-line text, sets the alignment between different lines. Options include LEFT, RIGHT, CENTER. | | 15 | **offvalue** The value of Checkbutton is not limited to 1 or 0; it can be other types of values. The state value of Checkbutton can be set via the onvalue and offvalue attributes. | | 16 | **onvalue** The value of Checkbutton is not limited to 1 or 0; it can be other types of values. The state value of Checkbutton can be set via the onvalue and offvalue attributes. | | 17 | **padx** Internal padding of the button in the x-axis direction, which is the distance between the button's content and its edge, default is 1 pixel. | | 18 | **pady** Internal padding of the button in the y-axis direction, default is 1 pixel. | | 19 | **relief** Border style, sets the 3D effect of the control. Options include: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. Default is FLAT. | | 20 | **selectcolor** Color after selection, default is selectcolor="red". | | 21 | **selectimage** Image after selection. | | 22 | **state** State, default is state=NORMAL. | | 23 | **text** Displayed text. Use "n" to wrap the text. | | 24 | **underline** Underline. By default, text on the button has no underline. The value is the index of the string with an underline; 0 means the first character is underlined, 1 means the first two characters are underlined, and so on. | | 25 | **variable** Variable; the value of variable is 1 or 0, representing selected or not selected. | | 26 | **width** The default width is determined by the text or image of the checkbutton; you can set a specific number of characters. | | 27 | **wraplength** Whether to set wrapping. | ### Methods The commonly used methods are as follows: | No. | Method & Description | | --- | --- | | 1 | **deselect()** Clears the selected option of the checkbutton. | | 2 | **flash()** Flashes the checkbutton between active and normal colors several times but maintains its initial state. | | 3 | **invoke()** This method can be called to perform the same action as when a user clicks the checkbutton to change its state. | | 4 | **select()** Sets the button to selected. | | 5 | **toggle()** Toggles between selected and not selected states. | ### Example In the example, clicking the button displays a message: ## Example from Tkinter import * import tkMessageBox import Tkinter top = Tkinter.Tk()CheckVar1 = IntVar()CheckVar2 = IntVar()C1 = Checkbutton(top, text = "", variable = CheckVar1, onvalue = 1, offvalue = 0, height=5, width = 20)C2 = Checkbutton(top, text = "GOOGLE", variable = CheckVar2, onvalue = 1, offvalue = 0, height=5, width = 20)C1.pack()C2.pack()top.mainloop() The test output is as follows: !(#) [![Image 3: Python GUI Programming](#) Python GUI Programming](#)
← Python Mongodb Delete DocumentBootstrap Make A Website β†’