( ** Python Certification Training: https://www.edureka.co/python ** )
This Edureka PPT on Tkinter tutorial covers all the basic aspects of creating and making use of your own simple Graphical User Interface (GUI) using Python. It establishes all of the concepts needed to get started with building your own user interfaces while coding in Python.
3. Python Certification Training https://www.edureka.co/python
Agenda
Introduction 01
Introduction
to Flask
Getting Started 02
Concepts 03
Practical Approach 04
Installing and working
with Flask
Looking at code to
understand theory
Overview of all the
concepts in Flask
5. Python Certification Training https://www.edureka.co/python
What Is A GUI?
GUI is a desktop app which helps you to interact with computersWhat is GUI?
GUI
Text Editors Games Apps
I’ll learn Tkinter!
6. Python Certification Training https://www.edureka.co/python
Why We Need GUI?
The graphical user interface, is a type of user interface that allows users to interact
with electronic devices through graphical icons and visual indicators
Graphical User InterfaceCommand Line
10. Python Certification Training https://www.edureka.co/python
Introduction to Flask
Tkinter in Python GUI Programming is standard Python GUI library
It gives us an object-oriented interface to the Tk GUI toolkit
12. Python Certification Training https://www.edureka.co/python
First Window Using Tkinter
Import the Tkinter module Create the GUI application main window
Add WidgetsEnter the main event loop
import tkinter
window = tkinter.Tk()
# to rename the title of the window
window.title("GUI")
# pack is used to show the object in
the window
label = tkinter.Label(window, text =
"Hello World!").pack()
window.mainloop()
14. Python Certification Training https://www.edureka.co/python
Adding Widgets To Our Application
A widget is an element of a graphical user interface (GUI) that displays information or provides
a specific way for a user to interact with the operating system or an application
Label Button Entry
ComboBox CheckButton Radio
ScrolledText SpinBox Menu Bar
Notebook
15. Python Certification Training https://www.edureka.co/python
Label Widget
Label You can set the label font so you can make it bigger and maybe bold
l1 = Label (window, text="edureka!“ font=("Arial Bold", 50))
l1.grid (column=0, row=0)
Changing the font style
and size
Output:
Example:
16. Python Certification Training https://www.edureka.co/python
Label Widget
Label We can set the default window size using geometry function
Example:
l1 = Label (window, text="edureka!“ font=("Arial Bold", 50))
window.geometry('350x200')
l1.grid (column=0, row=0)
The above line sets the window width to
350 pixels and the height to 200 pixels
Output:
17. Python Certification Training https://www.edureka.co/python
Button Widget
Button
Let’s start by adding the button to the window, the button is
created and added to the window the same as the label
Example:
Output:
bt = Button (window, text="Enter")
bt.grid (column=1, row=0)
Using grid function to
set the button position
18. Python Certification Training https://www.edureka.co/python
Button Widget
Button
• You can change foreground for a button or any other widget using fg property.
• Also, you can change the background color for any widget using bg property.
Example:
Output:
bt = Button (window, text="Enter", bg="orange", fg="red")
bt.grid (column=1, row=0)
Changing the background
and foreground color
19. Python Certification Training https://www.edureka.co/python
Button Widget
Button
• Let’s add button click event
• First, we will write the function that we need to execute when the button is clicked
Example:
def clicked():
l1.configure (text="Button was clicked !!")
bt = Button (window, text=“Enter”, command=clicked)
Function that will execute
the button click event
Wiring the button with the
function
20. Python Certification Training https://www.edureka.co/python
Entry Widget
Entry
In the previous Python GUI examples, we saw how to add simple widgets.
Now let’s try getting the user input using Tkinter Entry class (Tkinter textbox)
txt = Entry(window,width=10)
txt.grid(column=1, row=0)
def clicked():
res = "Welcome to " + txt.get()
l1.configure(text= res)
bt = Button (window, text=“Enter”, command=clicked)
Creating a textbox using
Tkinter Entry class
Once the button is clicked show
“Welcome to” concatenated with the
entered text
Example:
22. Python Certification Training https://www.edureka.co/python
Combobox Widget
Combobox Combobox Widgets are very easy to use and are widely used as well!
Example:
from tkinter.ttk import *
combo = Combobox(window)
combo['values']= (1, 2, 3, 4, 5, "Text")
combo.current(3)
combo.grid(column=0, row=0)
Adding the combobox items
using the tuple
Setting the selected item
24. Python Certification Training https://www.edureka.co/python
Checkbutton Widget
Checkbutton To create a checkbutton widget, you can use Checkbutton class
Example:
chk_state = BooleanVar()
chk_state.set (True)
chk = Checkbutton(window, text=‘Select', var=chk_state)
chk.grid(column=0, row=0)
Creating a variable of type BooleanVar which is not a
standard Python variable, it’s a Tkinter variable
Passing the chk_state to the Checkbutton
class to set the check state
26. Python Certification Training https://www.edureka.co/python
Radio Button Widget
Radio Button To add radio buttons, simply you can use RadioButton class
Example:
rad1 = Radiobutton(window, text=Python', value=1)
rad2 = Radiobutton(window, text=Java', value=2)
rad3 = Radiobutton(window, text=Scala', value=3)
rad1.grid(column=0, row=0)
rad2.grid(column=1, row=0)
rad3.grid(column=2, row=0)
You should set the value for every radio button
with a different value, otherwise, they won’t work
28. Python Certification Training https://www.edureka.co/python
ScrolledText Widget
ScrolledText To add a ScrolledText widget, you can use the ScrolledText class
from tkinter import scrolledtext
txt = scrolledtext.ScrolledText(window, width=40,height=10)
Here we specify the width and the height of the ScrolledText widget,
otherwise, it will fill the entire window
To set scrolledtext content, you can use the insert method - txt.insert(INSERT,'You text goes here')
Example:
29. Python Certification Training https://www.edureka.co/python
MessageBox Widget
MessageBox To show a message box using Tkinter, you can use messagebox library
Example:
from tkinter import messagebox
messagebox.showinfo('Message title’, 'Message content')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text=‘ENTER', command=clicked)
Example:
Shows a message box when the user clicks a button
31. Python Certification Training https://www.edureka.co/python
SpinBox Widget
SpinBox To create a Spinbox widget, you can use the Spinbox class
spin = Spinbox(window, from_=0, to=100, width=5)
Output:Example:
33. Python Certification Training https://www.edureka.co/python
Geometry Management
All tkinter widgets will have geometric measurements
pack() grid() place()
It organizes the
widgets in the block,
which mean it
occupies the entire
available width
It organizes the
widgets in table-like
structure
It's used to place the
widgets at a specific
position you want.
Geometry Manager Classes
35. Python Certification Training https://www.edureka.co/python
Organizing Layout And Widgets
We use Frame class to arrange layout in a window
Frame is used to create the divisions in the window. You can align the
frames as you like with side parameter of pack() method.
Button is used to create a button in the window. It takes several parameters like
text(Value of the Button), fg(Color of the text), bg(Background color)
Frame
Button
Let’s see some code
36. Python Certification Training https://www.edureka.co/python
Organizing Layout And Widgets
Sample Login Box
Grid is another way to organize the widgets. It
uses the Matrix row column concepts.
Grid
Let’s see more code
38. Python Certification Training https://www.edureka.co/python
Binding Functions
Calling functions whenever an event occurs
refers to a binding function.
Binding
I like code!
import tkinter
window = tkinter.Tk()
window.title("GUI")
# creating a function called say_hi()
def say_hi():
tkinter.Label(window, text = "Hi").pack()
tkinter.Button(window, text = "Click Me!", command = say_hi).pack()
window.mainloop()
40. Python Certification Training https://www.edureka.co/python
Event Handling
mousemove, mouseover, clicking, scrolling are some eventsEvents
<Button-1> <Button-2> <Button-3>
Left Click Middle Click Right Click
Mouse Buttons
42. Python Certification Training https://www.edureka.co/python
Images & Icons
Images can be added using the PhotoImage methodAdd Images
We love Edureka <3