GUI
Programming
(Python Tkinter)
What is Tkinter?
• Tkinter is the standard GUI library for Python.
• Tkinter provides a powerful object-oriented interface
to the Tk GUI toolkit.
How to create GUI Application
using Tkinter?
• Import the Tkinter module.
• Create the GUI application main window.
• Add widgets to the GUI application.
• Enter the main event loop to take action against each
event triggered by the user.
import tkinter
frame = tkinter.Tk()
# Codes to add widgets
frame.mainloop()
Note:
- In order to create a tkinter application, we generally create an
instance of tkinter frame through the Tk() method
- mainloop() tells Python to run the Tkinter event loop. This
method listens for events, such as button clicks or keypresses, and
blocks any code that comes after it from running until you close
the window where you called the method.
Tkinter Widgets
• Tkinter Widgets are various controls, such as labels,
text boxes and buttons, used in a GUI application.
https://www.tutorialspoint.com/python/python_gui_programming.htm
LABEL
• The Label widget is used to provide a single-line caption for
other widgets. It can also contain images.
ENTRY (TEXTBOX)
• The Entry widget is used to display a single-line text field for
accepting values from a user.
BUTTON
• The Button widget is used to display buttons in your
application.
Tkinter Widgets
TEXT (TEXTAREA)
• The Text widget is used to display text in multiple lines.
CHECKBUTTON
• The Checkbutton widget is used to display a number of options
as checkboxes. The user can select multiple options at a time.
RADIOBUTTON
• The Radiobutton widget is used to display a number of options
as radio buttons. The user can select only one option at a time.
COMBOBOX
• The Combobox widget is used to provides a dropdown list of
items from which a user can choose.
Label
• Simple syntax to create label:
• var = Label ( master, option, ... )
• Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options:
(https://www.tutorialspoint.com/python/tk_label.htm)
• These options can be used as key-value pairs separated by commas.
• Label example:
from tkinter import *
frame = Tk()
lblSample = Label(frame, text="This is an example of a
Label", width=50, height = 25)
lblSample.pack()
frame.mainloop()
Pack Layout Manager
• Tkinter has three Layout Managers that use geometric methods to position
widgets in an application frame: pack, place, grid.
• pack is the easiest Layout Manager to code with in Tkinter.
• Instead of declaring the precise location of a widget, the pack() method
declares the position of widgets in relation to each other.
• pack() organizes widgets in horizontal and vertical boxes that are limited to
left, right, top, bottom positions offset and relative to each other within a
frame.
• Syntax:
widget.pack( pack_options )
Entry
• Simple syntax to create entry:
• var = Entry ( master, option, ... )
• Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options:
https://www.tutorialspoint.com/python/tk_entry.htm
• These options can be used as key-value pairs separated by commas.
• Entry example:
from tkinter import *
frame = Tk()
L1 = Label(frame, text="Enter your name: ", height=5)
L1.pack(side = LEFT)
E1 = Entry(frame, bd=5)
E1.pack(side = RIGHT)
frame.mainloop()
Entry
Entry
Entry
Button
• Simple syntax to create button:
• var = Button ( master, option=value, ... )
• Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options:
https://www.tutorialspoint.com/python/tk_button.htm
• These options can be used as key-value pairs separated by commas.
• Button example:
from tkinter import *
frame = Tk()
L1 = Label(frame, text="Enter your name: ")
E1 = Entry(frame, bd=5)
B1 = Button(frame, text="CLEAR")
L1.pack()
E1.pack()
B1.pack()
frame.mainloop()
Button
Display Clear Demo
from tkinter import *
frame = Tk()
def Clear():
E1.delete(0,END)
E2.config(state=NORMAL)
E2.delete(0,END)
E2.config(state=DISABLED)
def Display():
E2.config(state=NORMAL)
E2.insert(0,"Hello " + E1.get())
E2.config(state= DISABLED)
Display Clear Demo
# widgets declarations
L1 = Label(frame, text="Enter your name: ")
E1 = Entry(frame, bd=5)
E2 = Entry(frame, bd=5)
B1 = Button(frame, text="CLEAR", command=Clear)
B2 = Button(frame, text="DISPLAY", command=Display)
# adding widgets
L1.pack()
E1.pack()
E2.pack()
B1.pack()
B2.pack()
#initializing widgets
E2.config(state= DISABLED)
frame.mainloop()
messagebox module
• The messagebox module is used to display message boxes in
your applications.
• This module provides a number of functions that you can use to
display an appropriate message.
• Syntax:
messagebox.FunctionName(title, message [, options])
messagebox module
messagebox module
from tkinter import *
from tkinter import messagebox
frame = Tk()
frame.geometry("300x200")
#geometry is a built-in method in tkinter which is used
to set the dimensions
lbl = Label(frame, text ='Message Box Examples', font =
"50")
lbl.pack()
messagebox.showinfo("showinfo", "Information")
messagebox.showwarning("showwarning", "Warning")
messagebox module
messagebox.showerror("showerror", "Error")
messagebox.askquestion("askquestion", "Are you sure?")
messagebox.askokcancel("askokcancel", "Want to
continue?")
messagebox.askyesno("askyesno", "Find the value?")
messagebox.askretrycancel("askretrycancel", "Try
again?")
frame.mainloop()
messagebox module
Entry box within a Messagebox
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter.messagebox import showinfo
frame=Tk()
name = askstring('Name', 'What is your name?')
showinfo('Greetings!', 'Hi, {}'.format(name),icon='warning')
# icon options: 'error', 'question', 'info'
frame.mainloop()
Display Clear Demo with message box
from tkinter import *
from tkinter import messagebox
from tkinter.simpledialog import askstring
frame = Tk()
def Clear():
ans = messagebox.askquestion("askquestion", "Are you sure?")
if ans == 'yes':
E1.delete(0,END)
E2.config(state=NORMAL)
E2.delete(0,END)
E2.config(state=DISABLED)
def Display():
age = askstring("Age","Enter your age:")
txt.set(E1.get() + " is " + age + " years old")
#set the string value
Display Clear Demo with message box
# widgets declarations
L1 = Label(frame, text="Enter your name: ")
E1 = Entry(frame, bd=5)
txt = StringVar() #declare textvariable as string
E2 = Entry(frame, bd=5, textvariable=txt, state='readonly’)
#set to readonly state
B1 = Button(frame, text="CLEAR", command=Clear)
B2 = Button(frame, text="DISPLAY", command=Display)
# adding widgets
L1.pack()
E1.pack()
E2.pack()
B1.pack()
B2.pack()
frame.mainloop()
Text
• Simple syntax to create text:
• var = Text ( master, option, ... )
• Parameters
• master − This represents the parent window.
• options − Here is the list of most commonly used options:
https://www.tutorialspoint.com/python/tk_text.htm
• These options can be used as key-value pairs separated by commas.
• Text example:
from tkinter import *
frame = Tk()
text = Text(frame)
text.insert(INSERT, "Hello! ") #The "INSERT" constant is a special index that
represents #the current cursor position in the Text widget
text.insert(END, "This is an example of TextArea")
text.pack()
text.tag_add("Hello", "1.0", "1.6")
text.tag_config("Hello", background="yellow", foreground="blue")
frame.mainloop()
Text
Text
• Text
widgets
support
three
distinct
helper
structures:
Marks, Tabs,
and Indexes
−
• Marks are
used to
bookmark
positions
between
two
characters
within a
given text.
Text
• Tags are
used to
associate
names to
regions of
text which
makes easy
the task of
modifying
the display
settings of
specific text
areas.
• Tags are
also used to
bind event
callbacks to
specific
ranges of
text.
Text with an Image
import tkinter as tk
def add_image():
text.image_create(tk. INSERT, image = img)
text.window_create(tk. INSERT, window = tk.Label(text, image=img))
#window_create()method allows you to insert other Tkinter widgets within the Text widget
frame = tk.Tk()
text = tk.Text(frame)
text.pack()
tk.Button(frame, text = "Insert", command = add_image).pack()
img = tk.PhotoImage(file = "myImage.gif")
frame.mainloop()
window_create method
from tkinter import Tk, Text, Button
# Create the main window
frame = Tk()
# Create a Text widget
text_widget = Text(frame)
text_widget.pack()
# Insert some text
text_widget.insert("insert", "Hello, world!")
# Create a button widget
button = Button(text_widget, text="Click Me!")
# Insert the button within the Text widget at index 1.6
text_widget.window_create("1.6", window=button)
# Start the main loop
frame.mainloop()
Thank you!