Python Gui Programming- Tkinter
Python Gui Programming- Tkinter
PROJECT DEVELOPMENT
USING PYTHON
MOUNT ZION COLLEGE OF ENGINEERING AND TECHNOLOGY
SATHYA. M, AP/CSE
24 HOURS WORKSHOP ON
PYTHON GUI PROGRAMMING WITH TKINTER
TKINTER INTRODUCTION
• Python provides the standard library Tkinter for creating the graphical user interface for
desktop based applications.
• Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in
Python.
3.Add the widgets like labels, buttons, frames, etc. to the window.
4.Call the main event loop so that the actions can take place on the user's computer screen.
Import module
Tkinter is the standard library.This can be imported as module in our project to use the
functionalities provides by the library for creating GUI in python and python based frameworks.
Syntax:
Creation of Button using tk themed widget (tkinter.ttk). This will give you the effects of modern
graphics. Effects will change from one OS to another because it is basically for the appearance.
Syntax: w = Button ( master, options)
root = Tk()
root.geometry('100x100')
# Create a Button
Tkinter widget state-It can have the following values: NORMAL, ACTIVE, and DISABLED.
Tkinter widget padding -The padx and pady attributes add extra horizontal and
vertical space to the widgets.
Tkinter Background colours It can be abbreviated to bg.
Likewise, the foreground colours of widgets can be set with foreground attribute. It can
be abbreviated to fg.
Width and height The width and height attributes set the width and height of the
widget.
Tkinter reliefs A relief is a border decoration. The possible values
are: SUNKEN, RAISED, GROOVE, RIDGE, and FLAT.
Tkinter fonts Tkinter has a tkinter.font module for working with fonts. It has some
built-in fonts such as TkTooltipFont, TkDefaultFont, or TkTextFont. The font is set
with the font attribute.
Tkinter cursors
The cursor is a small icon that shows where the mouse pointer is located. The cursor in
Tkinter is set with the cursor attribute.
Geometry management
• pack(),
• grid()
• place().
The Pack geometry manager packs widgets relative to the earlier widget. Tkinter literally packs all
the widgets one after the other in a window.
# Importing tkinter module
from tkinter import * from tkinter.ttk import *
# creating Tk window
master = Tk()
# creating a Fra, e which can expand according
# to the size of the window
pane = Frame(master)
pane.pack(fill = BOTH, expand = True)
# Button 2
# Execute Tkinter
master.mainloop()
OUTPUT
from tkinter import *
master = Tk()
# creating a Fra, e which can expand according to the size of the window
pane = Frame(master)
# Button 2
# Button 3
master.mainloop()
Canvas
The Canvas widget is used to draw shapes, such as lines, ovals, polygons and rectangles, in your
application. B. It serves as a blank canvas where you can create and manipulate graphical elements
dynamically.
Syntax:
root=Tk()
C= Canvas(root, height=500,width=500,bg="green")
C.pack()
line=C.create_line(0,0,600,500,fill="red",dash=(3,3),width=5)
rec=C.create_rectangle(150,125,450,375,fill="blue",width=5)
root.mainloop()
Output
Canvas1:tcanvas1.py
root = Tk()
C.pack()
mainloop()
output
CheckButton
The Checkbutton widget is used to present a set of options or choices to the user. It allows
the user to select one or multiple options by clicking on the checkboxes associated with
each option.
SYNTAX:
root = Tk()
root.geometry("300x200")
w.pack()
Checkbutton1 = IntVar()
Checkbutton2 = IntVar()
Checkbutton3 = IntVar()
Button2.pack()
Button3.pack()
mainloop()
output
CheckButton:tcheckbut1.py
root=Tk()
root.geometry("400x400")
ck1=IntVar()
ck2=IntVar()
ck3=IntVar()
def check():
if ck1.get() == 1:
elif ck2.get()==1:
elif ck3.get()==1:
else:
pass
chkbut1=Checkbutton(root,text="C",variable=ck1,onvalue=1,offvalue=0,height=2,width=10,bg="
red",fg="blue")
chkbut2=Checkbutton(root,text="python",
variable=ck2,onvalue=1,offvalue=0,height=2,width=10)
chkbut3=Checkbutton(root,text="Java", variable=ck3,onvalue=1,offvalue=0,height=2,width=10)
but=Button(root,text="ClicKHere", width=10,fg="yellow",command=check)
CheckButton:tcheckbut1.py
chkbut1.pack()
chkbut2.pack()
chkbut3.pack()
but.pack()
root.mainloop()
output
Entry
• The Entry Widget is a Tkinter Widget used to Enter or display a single line of text.
SYNTAX:
entry = tk.Entry(parent, options)
import tkinter as tk
root=tk.Tk()
root.geometry("600x400")
name_var=tk.StringVar()
passw_var=tk.StringVar()
def submit():
name=name_var.get()
password=passw_var.get()
print("The name is : " + name)
print("The password is : " + password)
name_var.set("")
passw_var.set("")
name_label = tk.Label(root, text = 'Username', font=('calibre',10, 'bold'))
name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))
passw_label = tk.Label(root, text = 'Password', font = ('calibre',10,'bold'))
passw_entry=tk.Entry(root, textvariable = passw_var, font = ('calibre',10,'normal'),
show = '*')
sub_btn=tk.Button(root,text = 'Submit', command = submit)
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
passw_label.grid(row=1,column=0)
passw_entry.grid(row=1,column=1)
sub_btn.grid(row=2,column=1)
root.mainloop()
Frame
SYNTAX:
frame = tk.Frame(root)
from tkinter import *
root = Tk()
root.geometry("300x150")
w = Label(root, text ='cse', font = "50")
w.pack()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
b1_button = Button(frame, text ="AI", fg ="red")
b1_button.pack( side = LEFT)
b2_button = Button(frame, text ="CLOUD", fg ="brown")
b2_button.pack( side = LEFT )
b3_button = Button(frame, text ="DS", fg ="blue")
b3_button.pack( side = LEFT )
b4_button = Button(bottomframe, text ="ML", fg ="green")
b4_button.pack( side = BOTTOM)
b5_button = Button(bottomframe, text ="OS", fg ="green")
b5_button.pack( side = BOTTOM)
b6_button = Button(bottomframe, text ="TOC", fg ="green")
b6_button.pack( side = BOTTOM)
root.mainloop()
OUTPUT
Listbox
The Listbox widget provides a way to display a list of items from which the user can select
one or more items.The Listbox widget will display the inserted items, and the user can
select one or more items from the list.
SYNTAX:
listbox = tk.Listbox(root)
from tkinter import *
top = Tk()
listbox = Listbox(top, height = 10,width = 15,bg = "grey",activestyle = 'dotbox',font
= "Helvetica",
fg = "yellow")
top.geometry("300x250")
label = Label(top, text = " FOOD ITEMS")
listbox.insert(1, "Nachos")
listbox.insert(2, "Sandwich")
listbox.insert(3, "Burger")
listbox.insert(4, "Pizza")
listbox.insert(5, "Burrito")
label.pack()
listbox.pack()
top.mainloop()
OUTPUT
Label
The Label widget is used to display text or images on the screen. It is a simple widget that
provides a way to show static content within a tkinter window or frame.
SYNTAX:
SYNTAX:
menu = tk.Menu(menubutton)
from tkinter import *
from tkinter.ttk import *
from time import strftime
root = Tk()
root.title('Menu Demonstration')
# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
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()
file.add_command(label ='Exit', command = root.destroy)
# Adding Edit Menu and commands
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, tearoff = 0)
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)
root.config(menu = menubar)
Menubutton
The Menubutton widget is used to create a drop-down menu with selectable options. It
provides a way to present a set of choices or actions to the user in a hierarchical menu
structure.
SYNTAX:
A Radio button is a widget used to present a set of mutually exclusive options to the user. It
allows the user to select only one option from a group of options.
SYNTAX:
The Message widget is used to display multiple lines of text in a formatted manner. It
provides a way to present longer messages or paragraphs of text within a tkinter window or
frame.
SYNTAX:
• The Scale widget is used to create a slider control that allows the user to select a value
within a specified range. It provides a way to interactively adjust and control numeric
values.
SYNTAX:
• The Scrollbar widget plays a crucial role in providing an interactive scrolling experience to
the user, ensuring easy access to the complete content of a widget.The Scrollbar widget is
used to provide scrolling functionality to other widgets that display more content than
their visible area can accommodate.
SYNTAX:
• scrollbar = tk.Scrollbar(root)
Text
• Text widget provides a way to display and edit multiline text content. It is a versatile
widget that allows the user to input and manipulate text data.
• The Text widget offers several features and functionalities, making it useful in various
scenarios.
Toplevel
SYNTAX:
• top_window = tk.Toplevel(root)
Spinbox
• Spinbox widget is used to provide a numerical input field with increment and decrement
buttons. It allows the user to select a value by either typing directly into the widget or by
using the up and
SYNTAX:
• PanedWindow widget is used to create a container that allows the user to resize and
adjust the size of its child widgets using a splitter or divider. It provides a way to create
resizable and adjustable panes within a window or frame.
SYNTAX:
• LabelFrame widget is used to create a container that groups and organizes other widgets
together. It provides a labeled border around its content, making it visually distinct and
allowing for better organization of related widgets.
SYNTAX:
SYNTAX:
• import tkinter as tk
• A database is a structured collection of data that is organized and stored in a way that
allows for efficient retrieval, management, and manipulation of information. It is
designed to store and manage large volumes of data, providing mechanisms for data
storage, retrieval, modification, and deletion.
Mysql
• In MySQL, data creation involves creating a database, defining tables with appropriate
columns and data types, and inserting data into those tables.
SYNTAX:
• In MySQL, table creation and definition are performed using Data Definition Language
(DDL) queries. DDL queries allow you to create, modify, and delete database objects such
as tables, indexes, and constraints.
name VARCHAR(50),
age INT,
email VARCHAR(100));
• DML queries
DML (Data Manipulation Language) queries in MySQL are used to manipulate and retrieve data stored within tables.
DML queries allow you to insert, update, delete, and retrieve data from the database.
• INSERT:
• UPDATE:
UPDATE your_table_name
WHERE condition;
DELETE:
• WHERE condition;
• Application with database connectivity