Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
96 views

GUI With Tkinter in Python Notes 2

Uploaded by

jincy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

GUI With Tkinter in Python Notes 2

Uploaded by

jincy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Checkbutton widget

• 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

Here is the simple syntax to create this widget −

w = Checkbutton (window, option, ... )

Parameters

• window − This represents the parent window.

• options − Here is the list of most commonly used options for this widget. These options can be

used as key-value pairs separated by commas.

PROGRAM:

from tkinter import *

import tkinter as tk

window = tk.Tk()

CheckVar1 = StringVar()

CheckVar2 = StringVar()

C1 = Checkbutton(window, text = "Music", variable = CheckVar1, onvalue ="Music")

C2 = Checkbutton(window,text = "Video", variable = CheckVar2, onvalue = "Video")


C1.pack()

C2.pack()

window.mainloop()

You can modify above code to display the chosen checkbox value in a message box by using the messagebox

module from tkinter.

Here's how you can do it:

1. Import the necessary modules.

2. Define a function to show the selected values when a button is clicked.

3. Add a button to trigger the function.

import tkinter as tk

from tkinter import *

from tkinter import messagebox


window = tk.Tk()

def show_values():

selected_value1 = CheckVar1.get()

selected_value2 = CheckVar2.get()

messagebox.showinfo("Selected Values", f"{selected_value1} {selected_value2}")

CheckVar1 = StringVar()

CheckVar2 = StringVar()

C1 = Checkbutton(window, text="Music", variable=CheckVar1, onvalue="Music",offvalue="")

C2 = Checkbutton(window, text="Video", variable=CheckVar2, onvalue="Video",offvalue="")

C1.pack()

C2.pack()

button = Button(window, text="Show Selected", command=show_values)

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.

By default, the values are 1 for onvalue and 0 for offvalue.

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

ones when a button is clicked:

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()

btn = Button(window, text="Show Selected", command=show_selected)


btn.pack()
window.mainloop()
RADIO BUTTONS

Radio buttons allow you to select between one of a number of mutually exclusive choices.

Typically, you use radio buttons together in a set.

They’re a good option if you have a few choices that you want users to select.

To create radio buttons, you use the Radiobutton widget.

PROGRAM:

import tkinter as tk
from tkinter import *
window = tk.Tk()

var = StringVar()
var.set('English')#default value

def cb():
print(var.get())

Radiobutton(window, text='English', variable=var, value='English', command=cb).pack()


Radiobutton(window, text='German', variable=var, value='German', command=cb).pack()
Radiobutton(window, text='French', variable=var, value='French', command=cb).pack()

window.mainloop()

LISTBOX WIDGET

A listbox displays a list of single-line text items, and allows users to browse through the list,

and selecting one or more items.


To add a listbox to a Tk window, create an instance of the tk.Listbox class.

Create Listbox

import tkinter as tk

from tkinter import *

window = tk.Tk()

window.title("Listbox in Tk")

listbox = Listbox()

listbox.pack()

window.mainloop()

Managing Items inside listbox

Add an item to the list via the insert() method:

import tkinter as tk

from tkinter import *

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:

# Add four elements to the end of the list.

import tkinter as tk

from tkinter import *

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

from tkinter import *

window = tk.Tk()

window.title("Listbox in Tk")

listbox = Listbox()

items = ("Python", "C", "C++", "Java")

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)

print(listbox.get(2)) # Prints C++

And delete() to remove it:

# Remove the C++ element.

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

program, or manipulating data.


• Toplevel menus are displayed just under the title bar of the root or any other

toplevel windows.

SYNTAX

menu = Menu(master, **options)

• When an application contains a lot of functions, you need to use menus to organize

them for easier navigation.


• Typically, you use a menu to group closely related operations. For example, you can find

the File menu in most text editors.


• Tkinter natively supports menus. It displays menus with the look-and-feel of the target

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')

Second, create a menu bar

menubar = Menu(window)

Note that each top-level window can only have only one menu bar.

Third, create a File menu whose container is the menubar:

file = Menu(menubar)

Fourth, add the File menu to the menubar:

menubar.add_cascade(label="File", menu=file)

Finally, add a menu item to the file_menu:

File.add_command(label='New File',command=None)
In this example, the label of the menu item is ‘New File’.

Assign it to the menu option of the root window:

window.config(menu=menubar)

Put it all together:

# importing only those functions which are needed

import tkinter as tk

from tkinter import *

# creating tkinter window

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)

menubar.add_cascade(label ='File', menu = file)

file.add_command(label ='New File', command = None)

file.add_command(label ='Open...', command = None)

file.add_command(label ='Save', command = None)

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).

** The add_separator method adds a separator line in the file menu.

‘’’

# Adding Edit Menu and commands

edit = Menu(menubar)

menubar.add_cascade(label ='Edit', menu = edit)


edit.add_command(label ='Cut', command = None)

edit.add_command(label ='Copy', command = None)

edit.add_command(label ='Paste', command = None)

edit.add_command(label ='Select All', command = None)

edit.add_separator()

edit.add_command(label ='Find...', command = None)

edit.add_command(label ='Find again', command = None)

# Adding Help Menu

help_ = Menu(menubar)

menubar.add_cascade(label ='Help', menu = help_)

help_.add_command(label ='Tk Help', command = None)

help_.add_command(label ='Demo', command = None)

help_.add_separator()

help_.add_command(label ='About Tk', command = None)

# 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

‘’’

You might also like