"Introduction to Tkinter: Python's GUI
Library"
A Simple and Effective Way to Build Desktop Applications.
What is Tkinter ?
Tkinter is Python's standard library for building Graphical User Interfaces
(GUIs).
It is simple, easy to use, and works on multiple platforms (Windows,
macOS, Linux).
Tkinter provides a variety of widgets like buttons, labels, text boxes,
menus and more that can be used to create interactive user interfaces.
Key Points:-
Part of Python’s standard library (no installation needed).
Based on the CLI/Tk GUI toolkit.
Key Features of Tkinter
Ease of Use: Tkinter is straightforward and beginner-friendly,
making it a great choice for learning GUI programming.
Standard Library: No need for extra installations, as Tkinter is
included with Python.
Cross-Platform: Tkinter applications work seamlessly on Windows,
macOS, and Linux.
Comprehensive Widgets: Tkinter provides a variety of widgets like
buttons, labels, entry boxes, text areas, checkboxes, radio buttons,
menus, and more.
Customizable: You can customize the appearance of widgets using
options and styles.
Event-Driven: Tkinter supports event-driven programming,
enabling you to create interactive applications that respond to user
actions.
Starting with Tkinter
Code Example :-
# Create main window
root = tk.Tk()
root.title("My First App")
root.geometry("400x300")
# Run the application
root.mainloop()
Explanation:-
tk.Tk(): Initializes the main window.
title(): Sets the window's title. -
geometry(): Defines the size of the window. -
mainloop(): Keeps the application running.
What are Widgets in Tkinter ?
In Tkinter, a widget is essentially a graphical component that the user can interact with.
They can range from simple elements like buttons and labels to more complex ones like
text entry fields, list boxes, and canvases. Each widget serves a specific purpose and can
be customized to fit the design and functionality requirements of your application.
Code Example:-
from tkinter import *
# create root window
root = Tk()
# frame inside root window Output:-
frame = Frame(root)
# geometry method
frame.pack()
# button inside frame which is inside root
button = Button(frame, text='Geek’)
button.pack()
# Tkinter event loop
root.mainloop()
Code Example :-
import tkinter as tk
root = tk.Tk()
root.title("Tkinter World")
label = tk.Label(root, text="Hello, Studytonight!")
label.pack()
entry = tk.Entry(root) Output :-
entry.pack()
button = tk.Button(root, text="Click Me!")
button.pack()
root.mainloop()
Geometry Managers
Tkinter provides many methods, one of them is the geometry() method. This method
is used to set the dimensions of the Tkinter window and is used to set the position of
the main window on the user’s desktop.
Code Example:-
# importing only those functions which are needed Output :-
from tkinter import Tk, mainloop, TOP
from tkinter.ttk import Button
# creating tkinter window
root = Tk()
# creating fixed geometry of the
# tkinter window with dimensions 150x200
root.geometry('200x150’)
# Create Button and add some text button = Button(root, text = 'Geeks’)
button.pack(side = TOP, pady = 5)
# Execute Tkinter
root.mainloop()
Add Style to tkinter button :-
Tkinter is a Python standard library that is used to create GUI (Graphical
User Interface) applications. It is one of the most commonly used
packages of Python. Tkinter supports both traditional and modern
graphics support with the help of Tk themed widgets. All the widgets
that Tkinter also has available in tkinter.ttk.
Adding style in a tkinter.ttk button is a little creepy because it doesn’t
support direct implementation. To add styling in a ttk.Button we have to
first create an object of style class which is available in tkinter.ttk.
We can create ttk.Button by using the following step :-
btn = ttk.Button(master , option –value , … )
To add styling on the ttk.Button we cannot directly pass the value in the
options. Firstly, we have to create a Style object which can be created as
follows :-
Style = ttk.style()
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Object
root = Tk() # Set geometry (widthxheight)
root.geometry('100x100’)
# This will create style object
style = Style()
# This will be adding style, and
# naming that style variable as
# W.Tbutton (TButton is used for ttk.Button).
style.configure('W.TButton', font =('calibri', 10, 'bold', 'underline'),foreground = 'red’)
# Style will be reflected only on
# this button because we are providing
# style only on this Button.
''' Button 1'‘’
btn1 = Button(root, text = 'Quit !',style = 'W.TButton',command =root.destroy)
btn1.grid(row = 0, column = 3, padx = 100) Output :-
''' Button 2'‘’
btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
# Import Required Module
from tkinter import *
from tkinter.ttk import *
# Create Root Object
root = Tk()
# Set Geometry(widthxheight)
root.geometry('500x500’)
# Create style Object
style = Style()
style.configure('TButton', font =('calibri', 20, 'bold'),borderwidth = '4’)
# Changes will be reflected
# by the movement of mouse.
style.map('TButton', foreground = [('active', '!disabled', 'green’)],
background = [('active', 'black')])
# button 1
btn1 = Button(root, text = 'Quit !', command = root.destroy)
btn1.grid(row = 0, column = 3, padx = 100) Output :-
# button 2btn2 = Button(root, text = 'Click me !', command = None)
btn2.grid(row = 1, column = 3, pady = 10, padx = 100)
# Execute Tkinter
root.mainloop()
Real-World use Cases :-
Advantages and Limitations :-
Advantages –
Ease of Use:Tkinter is straightforward and easy to learn, making it ideal
for beginners.
Built-in:Tkinter is included with Python, so no additional installation is
required.
Cross-platform:Tkinter works on Windows, macOS, and Linux, allowing
you to write code that runs on different operating systems.Fast
Development:You can quickly build simple GUIs with Tkinter's intuitive
syntax and widgets.
Large Community:Tkinter has a large and active community, which
means you can easily find help and resources online.
Disadvantages -
Look and Feel:The default look and feel of Tkinter widgets may not be as
modern as other GUI toolkits.
Advanced Features:Tkinter lacks some advanced features found in other
toolkits, such as complex animations or 3D graphics.
Performance:While Tkinter is suitable for most applications, it might not
be the best choice for very large or complex GUIs that require high
performance.
Complex Layouts:Creating complex layouts in Tkinter can become
cumbersome, especially compared to some other GUI toolkits that offer
more advanced layout managers.
Conclusion :-
Tkinter is a great choice for beginners to create functional GUI
applications.
While it may not be as feature-rich as other frameworks, it’s perfect for
small to medium projects.