In this article, we will see how to use GUI Modules in Python Tkinter with some coding snippets that will help you to understand the concept of why we use Tkinter.
Python provides several GUI (Graphical User Interface) development choices. Tkinter is the most widely use of all the GUI techniques. It is a standard Python interface to the Python-supplied Tk GUI toolkit. Python with Tkinter is the quickest and most straightforward approach to constructing GUI apps. Using Tkinter to create a GUI is a simple task.
To create a Tkinter app you need to follow these steps:
Importing the Tkinter module
Create the main window
Add any number of widgets to the main window
Apply the event Trigger on the widgets.
Importing Tkinter is the same as importing any other module in the Python code. Note that the name of the module in Python 2.0 to 2.9 is ‘Tkinter‘ and in Python 3 or upper version use ‘tkinter‘.
import tkinter
There are two main methods use that the user needs to remember while creating the Python application with GUI.
Method 01: Tk(screenName=None, baseName=None, className=’Tk’, useTk=1) – To create a main window, Tkinter offers a method ‘Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)’. To change the name of the window, you can change the className to the desired one. The basic code used to create the main window of the application is:
m=tkinter.Tk() #where m is the name of the main window object
Method 02: mainloop() – When your program is ready to launch, a procedure called mainloop() is called. The mainloop() is an endless loop that is use to execute the program. Wait for an event, and handle the event as long as the window is open.
m.mainloop()
Code Example:
import tkinter
m = tkinter.Tk()
# widgets are added here
m.mainloop()
Output: The window will be looks like this
Tkinter also provides access to the geometric configuration of widgets, which may be use to organize widgets in parent windows. There are primarily three geometry manager classes.
pack() method: It organizes the widgets in blocks before placing them in the parent widget.
grid() method: It organizes the widgets in a grid (table-like structure) before placing them in the parent widget.
place() method: It organizes the widgets by placing them in specific positions directed by the programmer.
Python Tkinter Widgets
Tkinter provides a variety of controls, such as buttons, labels, and text boxes, for use in a graphical user interface (GUI) application. These controls are frequently refer to as widgets. Tkinter now supports 17 widget types. We will discuss them one by one.
Python Tkinter Widget 01: Button
The Button widget is use to display buttons in your application.
Syntax:
w=Button(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the Buttons. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
activebackground: to set the background color when the button is under the cursor.
activeforeground: to set the foreground color when the button is under the cursor.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height: to set the height of the button.
Code Example:
import tkinter as tk
r = tk.Tk()
r.title('Testing Button Widget')
# After pressing button our code will be stop
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
Output:
Python Tkinter Widget 02: Canvas
The Canvas widget is use to draw shapes, such as lines, ovals, polygons, and rectangles, in your application.
Syntax:
w = Canvas(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used in the canvas.
highlightcolor: to set the color shown in the focus highlight.
width: to set the width of the widget.
height: to set the height of the widget.
Code Example:
from tkinter import *
master = Tk()
w = Canvas(master, width=40, height=60)
w.pack()
canvas_height=20
canvas_width=200
y = int(canvas_height / 2)
w.create_line(0, y, canvas_width, y )
mainloop()
Output:
Python Tkinter Widget 03: Checkbutton
The Checkbutton widget is use to display a number of options as checkboxes. The user can select multiple options at a time.
Syntax:
w = CheckButton(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
Title: To set the title of the widget.
activebackground: to set the background color when the widget is under the cursor.
activeforeground: to set the foreground color when the widget is under the cursor.
The Entry widget is use to display a single-line text field for accepting values from a user.
Syntax:
w=Entry(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
command: to call a function.
highlightcolor: to set the color shown in the focus highlight.
The Frame widget is use as a container widget to organize other widgets.
Syntax:
w = Frame(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
highlightcolor: To set the color of the focus highlight when the widget has to be focused.
bd: to set the border width in pixels.
bg: to set the normal background color.
cursor: to set the cursor used.
width: to set the width of the widget.
height: to set the height of the widget.
Code Example:
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack()
redbutton = Button(frame, text = 'Red', fg ='red')
redbutton.pack( side = LEFT)
greenbutton = Button(frame, text = 'Brown', fg='brown')
greenbutton.pack( side = LEFT )
bluebutton = Button(frame, text ='Blue', fg ='blue')
bluebutton.pack( side = LEFT )
blackbutton = Button(frame, text ='Black', fg ='black')
blackbutton.pack( side = LEFT)
root.mainloop()
Output:
Python Tkinter Widget 06: Label
The Label widget is use to provide a single-line caption for other widgets. It can also contain images.
Syntax:
w=Label(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the button.
width: to set the width of the button.
height: to set the height of the button.
Code Example:
from tkinter import *
root = Tk()
a = Label(root, text='softhunt.net tutorial website')
a.pack()
root.mainloop()
Output:
Python Tkinter Widget 07: Listbox
The Listbox widget is use to provide a list of options to a user.
Syntax:
w = Listbox(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
highlightcolor: To set the color of the focus highlight when the widget has to be focused.
The Menubutton widget is use to display menus in your application.
Syntax:
w = MenuButton(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
activebackground: To set the background when the mouse is over the widget.
activeforeground: To set the foreground when the mouse is over the widget.
bg: to set the normal background color.
bd: to set the size of the border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
highlightcolor: To set the color of the focus highlight when the widget has to be focused.
The Menu widget is use to provide various commands to a user. These commands are contained inside Menubutton.
Syntax:
w = Menu(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be passed as parameters. Some examples are shown below.
title: To set the title of the widget.
activebackground: to set the background color when the widget is under the cursor.
activeforeground: to set the foreground color when the widget is under the cursor.
The Message widget is use to display multiline text fields for accepting values from a user.
Syntax:
w = Message(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
bd: to set the border around the indicator.
bg: to set the normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
Code Example:
from tkinter import *
main = Tk()
Msg ='Hello Welcome to softhunt.net'
messageVar = Message(main, text = Msg)
messageVar.config(bg='lightblue', font='30px')
messageVar.pack( )
main.mainloop( )
Output:
Python Tkinter Widget 11: RadioButton
The Radiobutton widget is use to display a number of options as radio buttons. The user can select only one option at a time.
Syntax:
w = RadioButton(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
activebackground: to set the background color when the widget is under the cursor.
activeforeground: to set the foreground color when the widget is under the cursor.
bg: to set the normal background color.
command: to call a function.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the label in characters.
height: to set the height of the label in characters.
The Scale widget is use to provide a slider widget.
Syntax:
w = Scale(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: to set the background color when the widget is under the cursor.
bg: to set the normal background color.
orient: Set it to HORIZONTAL or VERTICAL according to the requirement.
from_: To set the value of one end of the scale range.
to: To set the value of the other end of the scale range.
image: to set the image on the widget.
width: to set the width of the label in characters.
Code Example:
from tkinter import *
master = Tk()
w = Scale(master, from_=0, to=100, orient=HORIZONTAL)
w.pack()
w = Scale(master, from_=0, to=100)
w.pack()
mainloop()
Output:
Python Tkinter Widget 13: Scrollbar
The Scrollbar widget is use to add scrolling capability to various widgets, such as list boxes.
Syntax:
w = Scrollbar(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: to set the background color when the widget is under the cursor.
bg: to set the normal background color.
width: to set the width of the widget.
bd: to set the size of the border around the indicator.
Code Example:
from tkinter import *
w = Tk()
scrollbar = Scrollbar(w)
scrollbar.pack( side = RIGHT, fill = Y )
mylist = Listbox(w, yscrollcommand = scrollbar.set )
for line in range(50):
mylist.insert(END, 'This is Python Tutorial' + str(line))
mylist.pack( side = LEFT, fill = BOTH)
scrollbar.config( command = mylist.yview )
mainloop()
Output:
Tkinter Widget 14: Text
The Text widget is use to display text in multiple lines.
Syntax:
w =Text(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
highlightcolor: To set the color of the focus highlight when the widget has to be focused.
insertbackground: To set the background of the widget.
bg: to set the normal background color.
font: to set the font on the button label.
image: to set the image on the widget.
width: to set the width of the widget.
height: to set the height of the widget.
Code Example:
from tkinter import *
w = Tk()
Txt = Text(w, height=2, width=30)
Txt.pack()
Txt.insert(END, 'Welcome to softhunt.net\nTUTORIAL WEBSITE\n')
mainloop()
Output:
Tkinter Widget 15: Toplevel
The Toplevel widget is use to provide a separate window container.
Syntax:
w = TopLevel(master, option=value)
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor.
width: to set the width of the widget.
height: to set the height of the widget.
Code Example:
from tkinter import *
master = Tk()
master.title('softhunt.net')
top = Toplevel()
top.title('Tkinter Python')
top.mainloop()
Output:
Tkinter Widget 16: SpinBox
The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be use to select from a fixed number of values.
Syntax:
w = SpinBox(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
activebackground: to set the background color when the widget is under the cursor.
disabledbackground: To disable the background when the mouse is over the widget.
bg: to set the normal background color.
bd: to set the size of the border around the indicator.
command: to call a function.
cursor: To appear the cursor.
width: to set the width of the label in characters.
from_: To set the value of one end of the range.
to: To set the value of the other end of the range.
Code Examples:
from tkinter import *
master = Tk()
w = Spinbox(master, from_ = 0, to = 50)
w.pack()
mainloop()
Output:
Tkinter Widget 17: PanedWindow
A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.
Syntax:
w = PannedWindow(master, option=value)
Parameters:
master is the parameter use to represent the parent window.
There are several options for changing the format of the widget. A number of choices, separated by commas, can be pass as parameters. Some examples are shown below.
bg: to set the normal background color.
bd: to set the size of the border around the indicator.