The checkbutton widget is used to choose between two distinct values (usually switching something on or off). Checkbuttons can contain text or images, and you can associate a Python function or method with each button. When the button is pressed, Tkinter calls that function or method.
The Checkbutton can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut. By default, the Tab key can be used to move to a button widget.
Each Checkbutton widget should be associated with a variable. Groups of checkbuttons can be used to implement “many-of-many” selections. To handle “one-of-many” choices, we use Radiobutton and Listbox widgets which we'll discuss in separate posts.
To use a Checkbutton, we must create a Tkinter variable which is a special kind of Tkinter variable, called an IntVar . To inspect the button state, we query this variable. The following program creates a Checkbutton:
from tkinter import *
root = Tk()
checkbuttonvariable = IntVar()
cb = Checkbutton(root, text="Tick", variable = checkbuttonvariable)
cb.pack()
mainloop()
The variable, checkbuttonvariable , will be 0 when the check button is unchecked and 1 when it is checked. To access the value of the variable, we need to use its get method, like this:
checkbuttonvariable.get()
We can also set the value of the variable using its set method. This will automatically check or uncheck the check button on the screen.
checkbuttonvariable.set(1)
checkbuttonvariable.set(0)
The following program creates the checkbutton and demonstrates the get() and set() methods:
from tkinter import *
root = Tk()
checkbuttonvariable = IntVar()
cb = Checkbutton(root, text="Tick", variable = checkbuttonvariable)
cb.pack()
print(checkbuttonvariable.get())
checkbuttonvariable.set(1)
print(checkbuttonvariable.get())
mainloop()
The output is shown below:
In the command prompt we see the following output:
0
1
------------------
(program exited with code: 0)
Press any key to continue . . .
Usually, checkboxes are shown on the screen as square boxes that can contain white spaces (for false, i.e not checked) or a tick mark or X (for true, i.e. checked).
A caption describing the meaning of the checkbox is usually shown adjacent to the checkbox. The state of a checkbox is changed by clicking the mouse on the box. Alternatively it can be done by clicking on the caption, or by using a keyboard shortcut, for example, the space bar.
We can use more than one checkbuttons in our program as shown below:
from tkinter import *
master = Tk()
Label(master, text="Your preference:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Python", variable=var1).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Java", variable=var2).grid(row=2, sticky=W)
mainloop()
The output is shown below:
Here I am ending today's post. Make some programs on your own as GUI needs a lot practice. Till we meet next keep practicing and learning Python as Python is easy to learn!
The Checkbutton can only display text in a single font, but the text may span more than one line. In addition, one of the characters can be underlined, for example to mark a keyboard shortcut. By default, the Tab key can be used to move to a button widget.
Each Checkbutton widget should be associated with a variable. Groups of checkbuttons can be used to implement “many-of-many” selections. To handle “one-of-many” choices, we use Radiobutton and Listbox widgets which we'll discuss in separate posts.
To use a Checkbutton, we must create a Tkinter variable which is a special kind of Tkinter variable, called an IntVar . To inspect the button state, we query this variable. The following program creates a Checkbutton:
from tkinter import *
root = Tk()
checkbuttonvariable = IntVar()
cb = Checkbutton(root, text="Tick", variable = checkbuttonvariable)
cb.pack()
mainloop()
The variable, checkbuttonvariable , will be 0 when the check button is unchecked and 1 when it is checked. To access the value of the variable, we need to use its get method, like this:
checkbuttonvariable.get()
We can also set the value of the variable using its set method. This will automatically check or uncheck the check button on the screen.
checkbuttonvariable.set(1)
checkbuttonvariable.set(0)
The following program creates the checkbutton and demonstrates the get() and set() methods:
from tkinter import *
root = Tk()
checkbuttonvariable = IntVar()
cb = Checkbutton(root, text="Tick", variable = checkbuttonvariable)
cb.pack()
print(checkbuttonvariable.get())
checkbuttonvariable.set(1)
print(checkbuttonvariable.get())
mainloop()
The output is shown below:
In the command prompt we see the following output:
0
1
------------------
(program exited with code: 0)
Press any key to continue . . .
Usually, checkboxes are shown on the screen as square boxes that can contain white spaces (for false, i.e not checked) or a tick mark or X (for true, i.e. checked).
A caption describing the meaning of the checkbox is usually shown adjacent to the checkbox. The state of a checkbox is changed by clicking the mouse on the box. Alternatively it can be done by clicking on the caption, or by using a keyboard shortcut, for example, the space bar.
We can use more than one checkbuttons in our program as shown below:
from tkinter import *
master = Tk()
Label(master, text="Your preference:").grid(row=0, sticky=W)
var1 = IntVar()
Checkbutton(master, text="Python", variable=var1).grid(row=1, sticky=W)
var2 = IntVar()
Checkbutton(master, text="Java", variable=var2).grid(row=2, sticky=W)
mainloop()
The output is shown below:
Here I am ending today's post. Make some programs on your own as GUI needs a lot practice. Till we meet next keep practicing and learning Python as Python is easy to learn!
0 comments:
Post a Comment