Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Tkinter Checkbutton

Python Tkinter Checkbutton

Python GUI Programming Python GUI Programming

The Python Tkinter Checkbutton is used to select the options we need. It has a small square box in front of it; when selected, a checkmark appears, and you can click again to remove the checkmark and deselect it.

Syntax

The syntax format is as follows:


w = Checkbutton ( master, option=value, ... )
No. Options & Description
1

activebackground

The background color of the button when the mouse hovers over it

2

activeforeground

The foreground color of the button when the mouse hovers over it

3

bg

The background color of the button

4

bitmap

Bitmap

5

bd

The size of the border, defaulting to 2 pixels

6

command

The associated function, executed when the button is clicked

7

cursor

The shape of the cursor, such as arrow, circle, cross, plus, etc.

8

disabledforeground

The foreground color of the disabled option

9

font

The text font

10

fg

The foreground color of the option

11

height

The number of text lines of the checkbutton, defaulting to 1.

12

highlightcolor

The highlight color when focused

13

image

Whether to use an icon

14

justify

When displaying multi-line text, set the alignment between different lines; options include LEFT, RIGHT, CENTER

15

offvalue

The value of a Checkbutton is not limited to 1 or 0; it can be other types of values. The state value of the Checkbutton can be set through the onvalue and offvalue properties.

16

onvalue

The value of a Checkbutton is not limited to 1 or 0; it can be other types of values. The state value of the Checkbutton can be set through the onvalue and offvalue properties.

17

padx

The padding of the button in the x direction, i.e., the distance between the button's content and its edge, defaulting to 1 pixel.

18

pady

The padding of the button in the y direction, defaulting to 1 pixel.

19

relief

Border style, setting the 3D effect of the widget. Options are: FLAT, SUNKEN, RAISED, GROOVE, RIDGE. The default is FLAT.

20

selectcolor

The color when selected, defaulting to selectcolor="red".

21

selectimage

The image when selected

22

state

The state, defaulting to state=NORMAL

23

text

The displayed text; use "\n" to wrap the text.

24

underline

Underline. By default, text on the button has no underline. The value is the string index to underline: when 0, the first character is underlined; when 1, the first two characters are underlined, and so on

25

variable

The variable; the value of variable is 1 or 0, representing selected or not selected

26

width

The default width is determined by the checkbutton's text or image; you can set a specific number of characters.

27

wraplength

Whether to enable wrapping.

Method

The following are commonly used methods:

No. Method & Description
1

deselect()

Clear the selected option of the checkbutton.

2

flash()

Blink the radio button several times between the active-state color and the normal color, but keep its original state.

3

invoke()

You can call this method to achieve the same action that occurs when the user clicks the radio button to change its state

4

select()

Set the button to selected.

5

toggle()

Toggle between selected and unselected states

Example

In the example, clicking the button displays a message:

Example

#!/usr/bin/python

-- coding: UTF-8 --

from

Tkinter

import

import

tkMessageBox

import

Tkinter

top

=

Tkinter

.

Tk

(

)

CheckVar1

=

IntVar

(

)

CheckVar2

=

IntVar

(

)

C1

=

Checkbutton

(

top

,

text

=

"

RUNOOB

"

,

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:

Python GUI Programming Python GUI Programming