Python GUI - Tkinter: To Create A Tkinter
Python GUI - Tkinter: To Create A Tkinter
Python GUI - Tkinter: To Create A Tkinter
Python offers multiple options for developing GUI (Graphical User Interface). Out of all
the GUI methods, tkinter is most commonly used method. It is a standard Python
interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the
fastest and easiest way to create the GUI applications. Creating a GUI using tkinter is an
easy task.
To create a tkinter:
1. Importing the module – tkinter
2. Create the main window (container)
3. Add any number of widgets to the main window
4. Apply the event Trigger on the widgets.
Importing tkinter is same as importing any other module in the python code. Note that
the name of the module in Python 2.x is ‘Tkinter’ and in Python 3.x is ‘tkinter’.
import tkinter
There are two main methods used you the user need to remember while creating the
Python application with GUI.
1. 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
2. mainloop(): There is a method known by the name mainloop() is used when you
are ready for the application to run. mainloop() is an infinite loop used to run the
application, wait for an event to occur and process the event till the window is not
closed.
m.mainloop()
import tkinter
m = tkinter.Tk()
'''
widgets are added here
'''
m.mainloop()
tkinter also offers access to the geometric configuration of the widgets which can
organize the widgets in the parent windows. There are mainly three geometry manager
classes class.
1. pack() method:It organizes the widgets in blocks before placing in the parent
widget.
2. grid() method:It organizes the widgets in grid (table-like structure) before placing
in the parent widget.
3. place() method:It organizes the widgets by placing them on specific positions
directed by the programmer.
There are a number of widgets which you can put in your tkinter application. Some of
the major widgets are explained below:
Button:To add a button in your application, this widget is used.
The general syntax is:
w=Button(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the Buttons.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
activebackground: to set the background color when button is under the
cursor.
activeforeground: to set the foreground color when button is under the
cursor.
bg: to set he 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.
import tkinter as tk
r = tk.Tk()
r.title('Counting Seconds')
button = tk.Button(r, text='Stop', width=25, command=r.destroy)
button.pack()
r.mainloop()
Output:
Canvas: It is used to draw pictures and other complex layout like graphics, text and
widgets.
The general syntax is:
w = Canvas(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed 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.
Entry:It is used to input the single line text entry from the user.. For multi-line text
input, Text widget is used.
The general syntax is:
w=Entry(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed 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.
width: to set the width of the button.
height: to set the height of the button.
Frame: It acts as a container to hold the widgets. It is used for grouping and
organizing the widgets. The general syntax is:
w = Frame(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
highlightcolor: To set the color of the focus highlight when 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.
root = Tk()
frame = Frame(root)
frame.pack()
bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )
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(bottomframe, text ='Black', fg ='black')
blackbutton.pack( side = BOTTOM)
root.mainloop()
Output:
Label: It refers to the display box where you can put any text or image which can be
updated any time as per the code.
The general syntax is:
w=Label(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
bg: to set he normal background color.
bg to set he 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.
Listbox: It offers a list to the user from which the user can accept any number of
options.
The general syntax is:
w = Listbox(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
bg: to set he normal background color.
bd: to set the border width in pixels.
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.
top = Tk()
Lb = Listbox(top)
Lb.insert(1, 'Python')
Lb.insert(2, 'Java')
Lb.insert(3, 'C++')
Lb.insert(4, 'Any other')
Lb.pack()
top.mainloop()
Output:
MenuButton: It is a part of top-down menu which stays on the window all the time.
Every menubutton has its own functionality. The general syntax is:
w = MenuButton(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
activebackground: To set the background when mouse is over the widget.
activeforeground: To set the foreground when mouse is over the widget.
bg: to set he normal background color.
bd: to set the size of 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 widget has to be
focused.
top = Tk()
mb = Menubutton ( top, text = "GfG")
mb.grid()
mb.menu = Menu ( mb, tearoff = 0 )
mb["menu"] = mb.menu
cVar = IntVar()
aVar = IntVar()
mb.menu.add_checkbutton ( label ='Contact', variable = cVar )
mb.menu.add_checkbutton ( label = 'About', variable = aVar )
mb.pack()
top.mainloop()
Output:
root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New')
filemenu.add_command(label='Open...')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=root.quit)
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
mainloop()
Output:
Message: It refers to the multi-line and non-editable text. It works same as that of
Label.
The general syntax is:
w = Message(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
bd: to set the border around the indicator.
bg: to set he 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.
Scale: It is used to provide a graphical slider that allows to select any value from that
scale. The general syntax is:
w = Scale(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
cursor: To change the cursor pattern when the mouse is over the widget.
activebackground: To set the background of the widget when mouse is over
the widget.
bg: to set he 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 widget.
Scrollbar: It refers to the slide controller which will be used to implement listed
widgets.
The general syntax is:
w = Scrollbar(master, option=value)
master is the parameter used to represent the parent window.
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
width: to set the width of the widget.
activebackground: To set the background when mouse is over the widget.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
Text: To edit a multi-line text and format the way it has to be displayed.
The general syntax is:
w =Text(master, option=value)
There are number of options which are used to change the format of the text.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
highlightcolor: To set the color of the focus highlight when widget has to be
focused.
insertbackground: To set the background of the widget.
bg: to set he 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.
TopLevel: This widget is directly controlled by the window manager. It don’t need
any parent window to work on.The general syntax is:
w = TopLevel(master, option=value)
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
width: to set the width of the widget.
height: to set the height of the widget.
SpinBox: It is an entry of ‘Entry’ widget. Here, value can be input by selecting a fixed
value of numbers.The general syntax is:
w = SpinBox(master, option=value)
There are number of options which are used to change the format of the widget.
Number of options can be passed as parameters separated by commas. Some of
them are listed below.
bg: to set he normal background color.
bd: to set the size of border around the indicator.
cursor: To appear the cursor when the mouse over the menubutton.
command: To call a function.
width: to set the width of the widget.
activebackground: To set the background when mouse is over the widget.
disabledbackground: To disable the background when mouse is over the
widget.
from_: To set the value of one end of the range.
to: To set the value of the other end of the range.
Syntax
1. messagebox.function_name(title, message [, options])
Parameters
o function_name: It represents an appropriate message box function.
o title: It is a string which is shown as a title of a message box.
o message: It is the string to be displayed as a message on the message box.
o options: There are various options which can be used to configure the message
dialog box.
The two options that can be used are default and parent.
Tkinter messagebox
The messagebox module is used to display the message boxes in the python
applications. There are the various functions which are used to display the relevant
messages depending upon the application requirements.
Syntax
1. messagebox.function_name(title, message [, options])
Parameters
o function_name: It represents an appropriate message box function.
o title: It is a string which is shown as a title of a message box.
o message: It is the string to be displayed as a message on the message box.
o options: There are various options which can be used to configure the message
dialog box.
The two options that can be used are default and parent.
1. default
The default option is used to mention the types of the default button, i.e. ABORT, RETRY,
or IGNORE in the message box.
2. parent
The parent option specifies the parent window on top of which, the message box is to be
displayed.
There is one of the following functions used to show the appropriate message boxes. All
the functions are used with the same syntax but have the specific functionalities.
1. showinfo()
The showinfo() messagebox is used where we need to show some relevant information to
the user.
Example
1. # !/usr/bin/python3
2.
3. from tkinter import *
4.
5. from tkinter import messagebox
6.
7. top = Tk()
8.
9. top.geometry("100x100")
10.
11. messagebox.showinfo("information","Information")
12.
13. top.mainloop()
Output:
2. showwarning()
This method is used to display the warning to the user. Consider the following example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3.
4. from tkinter import messagebox
5.
6. top = Tk()
7. top.geometry("100x100")
8. messagebox.showwarning("warning","Warning")
9.
10. top.mainloop()
Output:
3. showerror()
This method is used to display the error message to the user. Consider the following
example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.showerror("error","Error")
8. top.mainloop()
Output:
4. askquestion()
This method is used to ask some question to the user which can be answered in yes or
no. Consider the following example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askquestion("Confirm","Are you sure?")
8. top.mainloop()
Output:
5. askokcancel()
This method is used to confirm the user's action regarding some application activity.
Consider the following example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askokcancel("Redirect","Redirecting you to www.google.com")
8. top.mainloop()
Output:
6. askyesno()
This method is used to ask the user about some action to which, the user can answer in
yes or no. Consider the following example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askyesno("Application","Got It?")
8. top.mainloop()
Output:
7. askretrycancel()
This method is used to ask the user about doing a particular task again or not. Consider
the following example.
Example
1. # !/usr/bin/python3
2. from tkinter import *
3. from tkinter import messagebox
4.
5. top = Tk()
6. top.geometry("100x100")
7. messagebox.askretrycancel("Application","try again?")
8.
9. top.mainloop()
Output: