GUI With Tkinter in Python Notes 2
GUI With Tkinter in Python Notes 2
• The Checkbutton widget is used to display a number of options to a user as toggle buttons.
• The user can then select one or more options by clicking the button corresponding to each option.
• You can also display images in place of text.
Syntax
Parameters
• options − Here is the list of most commonly used options for this widget. These options can be
PROGRAM:
import tkinter as tk
window = tk.Tk()
CheckVar1 = StringVar()
CheckVar2 = StringVar()
C2.pack()
window.mainloop()
You can modify above code to display the chosen checkbox value in a message box by using the messagebox
import tkinter as tk
def show_values():
selected_value1 = CheckVar1.get()
selected_value2 = CheckVar2.get()
CheckVar1 = StringVar()
CheckVar2 = StringVar()
C1.pack()
C2.pack()
button.pack()
window.mainloop()
In Tkinter, the Checkbutton widget has two important options: onvalue and offvalue.
These options determine the value assigned to the associated variable when the checkbox is checked
or unchecked, respectively.
• onvalue: The value assigned to the variable when the checkbox is checked.
• offvalue: The value assigned to the variable when the checkbox is unchecked.
However, you can customize these values to be anything you want, such as specific strings.
Here's a simple Tkinter program that creates checkboxes for selecting your favorite pet animals and prints the chosen
import tkinter as tk
from tkinter import *
from tkinter import messagebox
window = tk.Tk()
def show_selected():
selected_pets = var1.get() + var2.get() + var3.get()
messagebox.showinfo("Selected Pets",f"{selected_pets}")
var1 = StringVar()
var2 = StringVar()
var3 = StringVar()
chk1 = Checkbutton(window, text="Dog", variable=var1, onvalue="Dog ", offvalue="")
chk2 = Checkbutton(window, text="Cat", variable=var2, onvalue="Cat ", offvalue="")
chk3 = Checkbutton(window, text="Bird", variable=var3, onvalue="Bird ", offvalue="")
chk1.pack()
chk2.pack()
chk3.pack()
Radio buttons allow you to select between one of a number of mutually exclusive choices.
They’re a good option if you have a few choices that you want users to select.
PROGRAM:
import tkinter as tk
from tkinter import *
window = tk.Tk()
var = StringVar()
var.set('English')#default value
def cb():
print(var.get())
window.mainloop()
LISTBOX WIDGET
A listbox displays a list of single-line text items, and allows users to browse through the list,
Create Listbox
import tkinter as tk
window = tk.Tk()
window.title("Listbox in Tk")
listbox = Listbox()
listbox.pack()
window.mainloop()
import tkinter as tk
window = tk.Tk()
window.title("Listbox in Tk")
listbox = Listbox()
listbox.insert(0, "Python")
listbox.pack()
window.mainloop()
The first argument specifies the position where you want to put the element, which can be any numeric value,
or tk.END to indicate the end of the list.
Every argument passed to insert() after the position will be added as an item to the listbox:
import tkinter as tk
window = tk.Tk()
window.title("Listbox in Tk")
listbox = Listbox()
listbox.insert(1,"Python”)
listbox.insert(2,"C”)
listbox.insert(3,"C++”)
listbox.insert(4,"Java”)
listbox.pack()
window.mainloop()
If the elements to be added are already stored in a list or tuple, it is better to do:
# Add elements stored in a list or tuple.
import tkinter as tk
window = tk.Tk()
window.title("Listbox in Tk")
listbox = Listbox()
listbox.insert(0, *items)
listbox.pack()
window.mainloop()
It is possible to know the number of items in a list via the size() function, which returns an integer.
items = (
"Python",
"C",
"C++",
"Java"
listbox.insert(0, *items)
print(listbox.size()) # Prints 4
Use the get() method to retrieve an item from the list if you know its position or index.
items = (
"Python",
"C",
"C++",
"Java"
listbox.insert(0, *items)
listbox.delete(2)
Python | Menu widget in Tkinter
• Menus are the important part of any GUI. A common use of menus is to provide
convenient access to various operations such as saving or opening a file, quitting a
toplevel windows.
SYNTAX
• When an application contains a lot of functions, you need to use menus to organize
platform that the program runs e.g., Windows, macOS, and Linux.
Creating a simple menu
First, create a root window and set its title to 'Menu Demo':
window= tk.Tk()
window.title('Menu Demo')
menubar = Menu(window)
Note that each top-level window can only have only one menu bar.
file = Menu(menubar)
menubar.add_cascade(label="File", menu=file)
File.add_command(label='New File',command=None)
In this example, the label of the menu item is ‘New File’.
window.config(menu=menubar)
import tkinter as tk
window =tk.Tk()
window.title('Menu Demonstration')
# Creating Menubar
menubar = Menu(window)
#This line creates a Menu object which will serve as the menubar for the
#application.
# Adding File Menu and commands
file = Menu(menubar)
file.add_separator()
‘’’
** The add_cascade method adds the file menu to the menubar with the label
"File".
** The add_command method adds menu items "New File", "Open...", and "Save"
to the file menu. Each item currently has no function (command = None).
‘’’
edit = Menu(menubar)
edit.add_separator()
help_ = Menu(menubar)
help_.add_separator()
# display Menu
window.config(menu = menubar)
‘’’
This final block of code:
• The config method of the root window sets its menu to the menubar we
created
‘’’