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 .
C:\Users\user name>pip install tk
Installation of Python is to be completed before installing Tkinter library . ( how to check Python installation is here. )
If you are getting error modulenotfounderror: no module named 'tkinter' then use the above steps to install Tkinter library.
What is my Tkinter Installed Version?
import tkinter
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.
C:\Users\user name>python -m tkinter
Show one blank window.
import tkinter as tk
my_w = tk.Tk()
my_w.geometry("500x500") # Size of the window
my_w.title("www.plus2net.com") # Adding a title
my_w.mainloop() # Keep the window open
Close window by using Esc
my_w.bind('<Escape>', lambda e:my_w.quit()) # to close.
my_w.destroy() Close the window
geometry()
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.
width,height=710,710 # set the variables
d=str(width)+"x"+str(height)
my_w.geometry(d)
Resizing of window is possible by default, but we can manage this setting by making resizing height, width to False
my_w.resizable(width=0,height=0) # resizing of window is not allowed
place for absolute and relative coordinates for layout management
ttkbootstrap
MySQL
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 »
Developing executable desktop applications by using PyInstaller in Tkinter Python library
Install PyInstaller by using Pip install.
pip install pyinstaller
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.
python3 -m PyIntaller tk-clock.py
This will create several files and directories at the same location. Your application will be inside the dist directory.
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 »