Python Unit 3 and 4-185-508-72-117
Python Unit 3 and 4-185-508-72-117
PROBLEM SOLVING
GUI With Tkinter
Prof. Sindhu R Pai
PCPS Theory Anchor - 2024
Department of Computer Science and Engineering
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Why GUI?
• A user with zero technical knowledge can still use a computer thanks to GUIs.
• GUIs provide an intuitive and visual method of interacting with software applications.
They simplify workflows and reduce the learning curve for new users.
• They don’t require the user to know any programming or memorize commands to
interact with the machine.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
1. Tkinter/ttkbootstrap
2. Qt for Python: PySide2 / Qt5
3. PySimpleGUI
4. PyGUI
5. Kivy
6. wxPython
7. Libavg
8. PyForms
9. Wax
10. PyGTK
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Tkinter
Basic Window
Output
Step 1: Import tkinter package
Step 2: root=tkinter.Tk()
Step 3: root.mainloop()
import tkinter
root = tkinter.Tk() #creates window
root.mainloop() #loops continuously until we close the window
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
mainloop()
• A function that continuously loops and displays the window till we
close it or an action closes the window.
• It will loop forever waiting for events from the user, until the user exits
the program (either by closing the window, or by terminating the program
with a keyboard interrupt in the console).
• All windows that are created work on this concept of constant looping
to keep track of the interactions of the user with the interface.
• It can track the movements of the mouse on the window because it
constantly loops and has knowledge of where the mouse pointer is on
the window at every frame.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
root.title(Title Name)
root.geometry(Dimension in widthxheight)
Example:
import tkinter
root = tkinter.Tk() #creates window
root.title("Tkinter Demonstration") #Title
root.geometry('500x500’) #Dimension
root.mainloop()
9
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Widgets
Widgets
• Steps to add widget to the Window
1. Create widget
2. Add it to the Window
• Creating a new widget doesn’t mean that it will appear on the screen. To
display it, we need to call a special method: either grid, pack, or place.
• Syntax
W = Button(parent,options)
• parent – parent window
• options – to change look of the buttons, written as comma-separated
16
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
import tkinter
from tkinter import *
from tkinter import messagebox
win = Tk()
win.title("Tkinter Button Widget Demonstration")
win.geometry('300x200')
def click():
messagebox.showinfo("Message", "Green Button clicked")
Button Widget
Example2 (Contd…)
Output
• Syntax
W = Canvas(parent,option=value)
• parent – parent window
• option – to change layout of the canvas, written as comma-separated-Key-values.
#creating canvas
cv=Canvas(win, bg = "orange", height = "300")
cv.pack()
win.mainloop()
21
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
import tkinter
win=tkinter.Tk()
win.title("Tkinter Canvas Widget")
# creating canvas
cv=tkinter.Canvas(win, bg="yellow", height=300, width=300)
# drawing two arcs
coord = 10, 10, 300, 300
arc1=cv.create_arc(coord, start=0, extent=150, fill="pink")
arc2=cv.create_arc(coord, start=150, extent=215, fill="green")
# adding canvas to window and display it
cv.pack()
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
cv.pack()
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
• Syntax
W = Checkbutton(parent,option=value)
• parent – parent window
• option – to configure checkbutton, written as comma-separated key-value pair.
Checkbutton Widget
• Functions
Checkbutton Widget
Example
from tkinter import *
win=Tk()
win.geometry("300x300")
Checkbutton Widget
Example (Contd…)
Checkbutton Widget
Example (Contd…)
cb1.pack()
cb2.pack()
cb3.pack()
mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
• Syntax
W = Label(parent,options)
• parent – parent window
• option – to configure the text, written as comma-separated-Key-value pair.
Label Widget
Example
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
• Syntax
W = Entry(parent,options)
• parent – parent window
• option – to configure the entry, written as comma-separated values.
Entry Widget
• Functions
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Dialogs in Tkinter
• Used to input data, modify data, change the application settings etc.
• By using the message box library, information from the application is displayed to
the user; this information can be classified into various types such as Error,
Warning, Cancellation etc.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Message Box
• Syntax
messagebox.function_name(Title, Message, [,options] )
function_name
Messagebox – askquestion()
Example 1
win.geometry("300x300")
# creating Submit Button
b=Button(win, text = "Submit", command = Submit)
b.pack()
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Example 1 Output
After clicking Submit button in the 1st window, 2nd window is displayed.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
frame=Frame(win)
frame.pack()
Example 1 (Continued)
bottomframe=Frame(win)
bottomframe.pack(side = BOTTOM )
win.mainloop()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
• Syntax
frame(parent)
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
# Frame 1
frame1=Frame(win,bg="black",width=500,height=300)
frame1.pack()
win.mainloop()
55
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
GUI - Tkinter
Explore further: