Tkinter is a standard Python library used for creating
graphical user interfaces (GUIs). It provides a set of tools and widgets for building desktop applications that can run on different platforms such as Windows, macOS, and Linux. Tkinter is based on the Tk GUI toolkit, which was originally developed for the Tcl programming language, but has been ported to other languages including Python. With Tkinter, developers can create interactive and intuitive user interfaces for their applications with features such as buttons, menus, text boxes, checkboxes, radio buttons, sliders, and many more.
How to install Tkinter Module
Tkinter comes with Python and while installing python check the tcl/tk and IDLE checkbox. To install Tkinter subsequently we have to use the command like this .
Installation of Python is to be completed before installing Tkinter library . ( how to
pip install tk check Python installation is here. )
If you are getting error then use the above steps to install Tkinter library.
modulenotfounderror: no module named 'tkinter' What is my Tkinter Installed Version?
print(tkinter.TkVersion) # 8.6
check tkinter installation
Run this command at your command prompt ( type cmd in your system.)
One small window will open showing you the details.
python -m tkinter
Show one blank window.
Close window by using
import tkinter as tk
my_w = tk.Tk()
# Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_w.mainloop() # Keep the window open Esc
my_w.bind('<Escape>', lambda e:my_w.quit()) # to close.
my_w.destroy() Close the window
my_w.geometry("400x350+410+100") # Size & Position
400 : width of the window
350 : Height of the window
410 : Position (opening ) of the window from left or X position
100 : Position (opening ) of the window from Top or Y position
Using height and width variables with geometry of the window. Inside our script we can use these variables.
Resizing of window is possible by default, but we can manage this setting by making resizing height, width to
width,height=710,710 # set the variables
While opening we can set to full screen
my_w.resizable(width=0,height=0) # resizing of window is not allowed
my_w.state('zoomed') # default is 'normal'
More about managing geometry →
Python GUI basic window code using Tkinter library for displaying first blank window
Change the background colour
To above code we will add different components.
List of widgets in Tkinter
askopenfile Dialog box to show file browser & upload file from local system
askopenfile Upload and display images using file browser
Button Add button with click events and style
Canvas Draw Line, Arc, Oval, Polygon ,rectangle etc
Checkbutton Read data and manage (set or get) of a checkbutton
Combobox Managing options of a dropdown Combobox
colors Tkinter supported colors list
colorchooser access the native colour picker dialog
config() access the access the object's attributes after its initialization
Entry Add Text box , Single line text entry
Events Mouse and Keyboard Events using Call back functions
frame Group widgets and add separator to window
image Displaying icon and Images on Window
Label Add Label and change text & other attributes
LabelFrame Group Widgets and add Label
Listbox Read selection , add elements to a listbox
MessageBox Show Message box and take user choice
Menu Menu with option and methods
Notebook Methods and options to manage Tabs
OptionMenu Drop down box to select one of many options
PanedWindow Container allowing user to adjust the size by using the mouse
Progressbar Displaying status of process with options and methods
Radiobutton Read data and manage (set or get) of a Radio button
Scale Use slider to fix value
Scrollbar Scrollbar options and uses
simpledialog Dialog box to take user input.
StringVar tk.StringVar() and trace with callback functions
Spinbox User selection from a range of options
Text Add Text box , Multi line text entry
Toplevel Display pop-up or child window from Parent
Parent-child Parent Child node of Treeview
Treeview Basics of Treeview
Treeview Adding row to Treeview using insert()
Treeview Adding row to Treeview after inserting data to MySQL table
validation Validating user inputs
clock Display time with Date
Calendar Display Date picker
DateEntry Display Drop-down Date entry Layout of widgets in Tkinter window
grid Add grid for layout management
pack Pack for layout management
place place for absolute and relative coordinates for layout management ttkbootstrap
We can connect to MySQL database from Python and manage the data. Tkinter adds the GUI capability to this data handing capacity. We will learn about adding data, displaying, updating etc by using standard SQL.
Python to MySQL connection and handling data
MySQL Displaying records from MySQL database in Tkinter window
Treeview Displaying records from MySQL database in Tkinter window using Treeview
Delete Deleting selected row from MySQL table and Treeview
Pagination Pagination of records from MySQL table using Treeview
Paging Pagination of records of MySQL table using Tkinter window
record display Displaying record in a Tkinter window by taking user input as row id
record update Select-Edit-Update row by using Treeview
Image display Displaying Binary data from MySQL Blob column.
Image display Displaying all records with Images from MySQL Blob column.
Image add Inserting uploaded image to Blob column of MySQL table
Image add Adding data to student table with Photo using Blob column of MySQL table
Image Update Updating user uploaded image in MySQL Blob column
record add Adding user entered data through Tkinter window to MySQL table
Login System for Login with add user and listing users with delete option
Exercise Exercise on Tkinter and Managing MySQL database SQlite
SQLite is a file based database with full functional capacity like any other Relational Database. Portability is the main advantage of SQLite database. Using sqlite3 library we can manage SQLite database by using Standard Query languages. By using Tkinter we can add GUI capability to manage SQLite database from Python.
Python to SQLite connection and handling data
» Without installing use SQLite database from google Drive by using Colab.
Display Display records from SQLite table in Tkinter window
Display Paging : Breaking of number of records to pages
Delete Delete record on button click and after user confirmation
record display Return details of the record by entering row ID
record add Insert user entered data through Tkinter window to SQLite table
OptionMenu Unique data from SQLite as OptionMenu list
Two OptionMenus Two dependant OptionMenus ( Category and subcategory)
Blob Column Managing Blob column, changes required in Script managing MySQL database
SQLite Connector Application to Manage Database and create new database Dynamic Graphs in Tkinter GUI
Display Pandas DataFrame graphs in Tkinter
To list all available methods of any widget
l3 = tk.Label(my_w, text='Welcome')
object_methods=[l3 for l3 in dir(tk.Label)
Download .zip file with .ipynb files of Video Tutorials ⇓
Time delay using after()
l1 is one Label, this code will update the text on Label l1 after 3000 millseconds ( 3 seconds ).
Creating windows EXE file from Tkinter
Developing executable desktop applications by using PyInstaller in Tkinter Python library
Install PyInstaller by using Pip install.
From command prompt move to the directory where your source file is there ( this is required if path is not set ) . You can run from your existing prompt also.
pip install pyinstaller
This will create several files and directories at the same location. Your application will be inside the
python3 -m PyIntaller tk-clock.py
Creating single application file
python3 -m PyInstaller --onefile --windowed tk-clock.py
importat tips for application development
Don’t initialize any widgets inside a function. Start them from root of your script and manage the options (attributes) from inside the functions by using config() method.
Keep the common requirements like Database connection string in a common file and call them from different scripts. This helps in changing the login details in one location when you shift to different database.
If you have common logo, background colour etc. then store them inside config.py file ( can use any other name ) and call them from different scripts. Easy maintenance.
Project : GUI Text Editor
Text Editor with all file handling operations like New, Open., Save, Save As and Close to add or update data of the file.
The tkinter filedialog is used to display file handling dialog boxes and Menu is used to execute various functions to manage a file.
Tkinter Text Editor to Manage File Operations
» QR code generator
Projects using Tkinter
← Subscribe to our YouTube Channel here