Tkinter, an integral part of Python's standard library, is a powerful tool for crafting graphical user interfaces (GUIs). This library equips developers with an array of widgets and tools necessary for building robust desktop applications, ensuring seamless functionality across various platforms like Windows, macOS, and Linux. Rooted in the Tk GUI toolkit, originally designed for the Tcl programming language, Tkinter has been adeptly adapted for Python, enhancing its versatility.
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.
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.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
The mainloop() function is what keeps a Tkinter application running and responsive. It's the heart of any Tkinter application, it handles events, user interactions, and rendering updates to the GUI. Without calling mainloop(), a Tkinter application would open a window and immediately close it, or not open it at all, since the program would reach its end without waiting for user interaction.
Check the two print commands here, after closing of the Tkinter window the last line will be printed. The line just before the last print command my_w.mainloop() will make the program wait for user interactions and will allow further execution ( below it ) once the window is closed.
import tkinter as tk
my_w = tk.Tk() # root window
my_w.geometry("400x250") # Size of the window
print( ' I am before mainloop ')
my_w.mainloop() # Keep the window open
print(' I am after mainloop')
Explore how the application behaves by toggling the my_w.mainloop() line: comment it out to observe the effect, then uncomment it to see the differences in application functionality.
ttk, which stands for Themed Tk, is a newer addition to Tkinter that provides access to the themed widget set introduced in Tk 8.5. This module allows for the creation of widgets that can more closely match the native look and feel of a user's operating system.
import tkinter as tk
from tkinter import ttk
ttk comes with 18 widgets, twelve of which already existed in tkinter:
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 »
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. Display records from SQLite table in Tkinter window »
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.
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 »