How to Use GUI Modules in Python Tkinter

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.

Introduction to Python 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 window

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.

  1. pack() method: It organizes the widgets in blocks before placing them in the parent widget.
  2. grid() method: It organizes the widgets in a grid (table-like structure) before placing them in the parent widget.
  3. 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:

tkinter button widget

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:

tkinter canvas widget

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.
    • bg: to set the normal backgrouSteganography.
    • Attach a File: nd color.
    • command: to call a function.
    • font: to set the font on the button label.
    • image: to set the image on the widget.

Code Example:

from tkinter import *
master = Tk()
var01 = IntVar()
Checkbutton(master, text='male', variable=var01).grid(row=0, sticky=W)
var02 = IntVar()
Checkbutton(master, text='female', variable=var02).grid(row=1, sticky=W)
var03 = IntVar()
Checkbutton(master, text='other', variable=var03).grid(row=2, sticky=W)
mainloop()

Output:

tkinter checkbutton widget

Python Tkinter Widget 04: Entry

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.
    • width: to set the width of the button.
    • height: to set the height of the button.

Code Example:

from tkinter import *
master = Tk()
Label(master, text='First Name').grid(row=0)
Label(master, text='Last Name').grid(row=1)
Label(master, text='Address').grid(row=2)
Label(master, text='ZIP Code').grid(row=3)
entry1 = Entry(master)
entry2 = Entry(master)
entry3 = Entry(master)
entry4 = Entry(master)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
entry3.grid(row=2, column=1)
entry4.grid(row=3, column=1)
mainloop()

Output:

tkinter entry widget

Python Tkinter Widget 05: Frame

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:

tkinter frame widget

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:

tkinter label widget

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.
    • bg: to set the 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.

Code Example:

from tkinter import *
langauges = Tk()
listbox = Listbox(langauges)
listbox.insert(1, 'Java')
listbox.insert(2, 'JavaSript')
listbox.insert(3, 'Python')
listbox.insert(4, 'PHP')
listbox.insert(5, 'C++')
listbox.pack()
langauges.mainloop()

Output:

tkinter listbox widget

Python Tkinter Widget 08: MenuButton

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.

Code Example:

from tkinter import *
menubar = Tk()
bar = Menubutton ( menubar, text = 'Contact Details')
bar.grid()
bar.menu = Menu ( bar, tearoff = 0 )
bar['menu'] = bar.menu
cVar = IntVar()
aVar = IntVar()
bar.menu.add_checkbutton ( label ='[email protected]', variable = cVar )
bar.menu.add_checkbutton ( label = 'LinkedIn "softhunt.net"', variable = aVar )
bar.pack()
menubar.mainloop()

Output:

tkinter menubutton widget

Python Tkinter Widget 09: Menu

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.
    • 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.

Code Example:

from tkinter import *
m = Tk()
menu = Menu(m)
m.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New Text File')
filemenu.add_command(label='New File...')
filemenu.add_command(label='New Window')
filemenu.add_separator()
filemenu.add_command(label='Open File...')
filemenu.add_command(label='Open Folder...')
filemenu.add_command(label='Open Recent')
filemenu.add_separator()
filemenu.add_command(label='Save')
filemenu.add_command(label='Save As...')
filemenu.add_command(label='Save All')
filemenu.add_separator()
filemenu.add_command(label='Exit', command=m.quit)
helpmenu = Menu(menu)
menu.add_cascade(label='Help', menu=helpmenu)
helpmenu.add_command(label='About')
mainloop()

Output:

tkinter menu widget

Python Tkinter Widget 10: Message

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:

message widget

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.

Code Example:

from tkinter import *
root = Tk()
var = IntVar()
Radiobutton(root, text='Python', variable=var, value=1).pack(anchor=W)
Radiobutton(root, text='Tkinter', variable=var, value=2).pack(anchor=W)
Radiobutton(root, text='WordPress', variable=var, value=3).pack(anchor=W)
Radiobutton(root, text='Gutenberg', variable=var, value=4).pack(anchor=W)
mainloop()

Output:

radiobutton widget

Python Tkinter Widget 12: Scale

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:

scale widget

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:

scrollbar widget

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:

text widget

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:

toplevel widget

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:

spinbox widget

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.
    • 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 *
pan1 = PanedWindow()
pan1.pack(fill = BOTH, expand = 1)
left = Entry(pan1, bd = 5)
pan1.add(left)
pan2 = PanedWindow(pan1, orient = VERTICAL)
pan1.add(pan2)
top = Scale( pan2, orient = HORIZONTAL)
pan2.add(top)
mainloop()

Output:

panedwindow widget

Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How Long Does it Take to Learn Python? (2022 Guide + Tips for Learning)

Leave a Comment