Checkbutton in Tkinter tutorial

Source: Internet
Author: User
Tags tkinter tutorial
# 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.