# Checkbutton for Tkinter tutorials
# Checkbutton, also known as the multiple-choice button, can represent two states: On and Off. You can set a callback function. When you click this button, the callback function is called.
'''1. Simple Checkbutton example '''
# Create a Checkbutton and display the text as "python"
From Tkinter import *
Root = Tk ()
Checkbutton (root, text = 'python'). pack ()
Root. mainloop ()
'''2. Set the callback function of the Checkbutton '''
From Tkinter import *
Def callCheckbutton ():
Print you check this button'
Root = Tk ()
Checkbutton (root, text = 'check python', command = callCheckbutton). pack ()
Root. mainloop ()
# This callback function will be called regardless of the Checkbutton status
''' 3. Use the callback function to change the value of the displayed text of Checkbutton '''
From Tkinter import *
Def callCheckbutton ():
# Change the value of v, that is, change the display value of Checkbutton.
V. set ('check Tkinter ')
Root = Tk ()
V = StringVar ()
V. set ('check python ')
# Textvariable, the property of binding v to Checkbutton
Checkbutton (root, text = 'check python', textvariable = v, command = callCheckbutton). pack ()
Root. mainloop ()
'''4. the preceding textvariable method is identical to the Button method. In this example, another attribute variable of Checkbutton is used to distinguish it from textvariable, which is bound to the control itself, checkbutton has its own values: On and Off. The default status is 1 and Off is 0, for example :'''
# Display the Checkbutton Value
From Tkinter import *
Root = Tk ()
# Bind an integer to the Checkbutton value. Each time you click Checkbutton, the current value is printed.
V = IntVar ()
Def callCheckbutton ():
Print v. get ()
Checkbutton (root,
Variable = v,
Text = 'checkbutton value ',
Command = callCheckbutton). pack ()
Root. mainloop ()
'''5. the Checkbutton value is not only 1 or 0. It can be another type of value. You can set the status value of Checkbutton through the onvalue and offvalue attributes. The following code sets On to 'python ', when the Off value is set to 'tkinter, the print value of the program is no longer 0 or 1, but 'tkinter 'or 'python ''''
From Tkinter import *
Root = Tk ()
# Bind a string with the Checkbutton value. Each time you click Checkbutton, the current value is printed.
V = StringVar ()
Def callCheckbutton ():
Print v. get ()
Checkbutton (root,
Variable = v,
Text = 'checkbutton value ',
Onvalue = 'python', # Set the On Value
Offvalue = 'tkinter ', # Set the Off Value
Command = callCheckbutton). pack ()
Root. mainloop ()
#6. You can use other properties fg/bg/relief/width/height/justify/state in the same way as Button.
# Author: jcodeer
# Blog: jcodeer.cublog.cn
# Email: jcodeer@126.com