The Wayback Machine - https://web.archive.org/web/20241003005958/https://www.geeksforgeeks.org/python-tkinter-tutorial/
Open In App

Python Tkinter Tutorial

Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Tkinter is the most commonly used library for developing GUI (Graphical User Interface) in Python. It is a standard Python interface to the Tk GUI toolkit shipped with Python. As Tk and Tkinter are available on most of the Unix platforms as well as on the Windows system, developing GUI applications with Tkinter becomes the fastest and easiest.

Image

Getting Started with Tkinter

In this guide, we’ll walk you through the essentials of Tkinter, from installation to creating your first GUI application. We’ll explore the concept of widgets, learn how to create basic GUI elements, and even dive into more advanced topics like destroying windows and gaining an overview of Tkinter in Python.

Basic Widgets in Tkinter

In this Tkinter section, we’ll explore the fundamental building blocks of What are Widgets in Tkinter?, Creating Interactive applications using Tkinter’s, using basic widgets. From simple frames and labels to more complex elements like scrollable frames and treeviews, we’ll cover a wide range of widgets and their customization options.

  1. Label: Display static text or images.
  2. Button: Create clickable buttons to trigger actions.
  3. Entry: Allow user input for single-line text.
  4. Frame: Organize widgets within a container.
  5. Checkbutton: Create checkboxes for boolean options.
  6. Radiobutton: Create radio buttons for mutually exclusive options.
  7. Listbox: Display a list of items for selection.
  8. Scrollbar: Add scrollbars to widgets like Listbox.
  9. Menu: Create simple menus for application navigation.
  10. Canvas: Draw shapes, lines, text, and images.

Intermediate Widgets in Tkinter

  1. Combobox: Provide a dropdown list with editable text entry.
  2. Scale: Create a scale widget for selecting values within a range.
  3. Toplevel: Create additional windows/dialogs.
  4. Message: Display simple messages or notifications.
  5. Menubutton: Create a button that opens a menu.
  6. Progressbar: Show progress of a task.
  7. Spinbox: Provide a numerical input with up/down arrows.

Advanced Widgets in Tkinter

  1. ScrolledText Widget: Create a text widget with built-in scrollbars.
  2. Treeview: Display hierarchical data in a tree-like structure.
  3. MessageBox Widget: Display dialog boxes for messages, warnings, etc.
  4. Treeview scrollbar Widget: Add scrollbars to Treeview widgets.
  5. Text: Create a multiline text input with advanced editing capabilities.

Geometry Management in Tkinter

Efficiently managing the geometry of Tkinter windows and widgets is essential for creating polished user interfaces. In this section, we’ll cover everything from setting window dimensions to handling resizing, padding, and widget positioning. We’ll explore methods like place(), grid(), and pack() to help you create organized and responsive layouts. Additionally, we’ll delve into advanced techniques like the PanedWindow widget.

Fonts, Colors, Images in Tkinter

The choice of fonts, colors, and images can make a significant impact on the user experience and the overall aesthetic appeal of an application. In this section, we will delve into the intricacies of customizing fonts, exploring a variety of color options, and incorporating images into your projects.

Canvas

Tkinter Advance

Applications and Projects

Python Tkinter Tutorial – FAQs

What is Tkinter in Python Used For?

Tkinter is the standard GUI (Graphical User Interface) toolkit in Python. It is used to create desktop applications that are easy to use and visually appealing. Tkinter provides various controls, such as buttons, labels, text boxes, checkboxes, and more, which are used to build interactive applications. It is widely appreciated for its simplicity and serves as a straightforward way to create GUI applications quickly.

How to Run Tkinter Program in Python?

To run a Tkinter program, you need to import the Tkinter module (usually imported as tkinter in Python 3), create a GUI application main window, and then add widgets and event bindings as needed. Here is a basic example to get started:

import tkinter as tk

def main():
# Create the main window
root = tk.Tk()

# Create a label widget
label = tk.Label(root, text='Hello, Tkinter!')
label.pack()

# Start the GUI event loop
root.mainloop()

if __name__ == "__main__":
main()

In this script, a window containing a single label saying “Hello, Tkinter!” is created, and mainloop() keeps the window open.

Which GUI is Best for Python?

The choice of GUI toolkit depends on your specific needs:

  • Tkinter: Best for simple and small applications, comes with Python, making it easy to get started without installing anything else.
  • PyQt or PySide: Good for creating professionally looking applications, offers more widgets and advanced functionalities than Tkinter. Suitable for commercial and larger applications.
  • wxPython: Another powerful option for creating cross-platform desktop applications, known for its native look on each platform.
  • Kivy: Best for applications on touch-enabled devices. It’s suitable for both mobile and desktop applications that require multitouch features.

Who Invented Tkinter?

Tkinter is based on the Tk GUI toolkit, which was developed by John Ousterhout while at the University of California, Berkeley. Tk was originally designed for the Tcl scripting language but has since been adapted for use with Python through Tkinter.

What are the Benefits of Using Tkinter?

The benefits of using Tkinter include:

  • Simplicity: Tkinter is simple to learn and use, making it an excellent choice for beginning programmers and small-scale applications.
  • Bundled with Python: It is included with most Python installations, so no additional installation is necessary to get started.
  • Cross-platform: Tkinter works on Windows, macOS, and Linux, providing a uniform look across all platforms.
  • Sufficient for most needs: For many applications, Tkinter provides enough widgets and functionality without the need for third-party libraries.
  • Community and Documentation: There is a large community of users and extensive documentation available to help solve common and uncommon issues that might arise.


Similar Reads

Difference Between The Widgets Of Tkinter And Tkinter.Ttk In Python
Python offers a variety of GUI (Graphical User Interface) frameworks to develop desktop applications. Among these, Tkinter is the standard Python interface to the Tk GUI toolkit. Tkinter also includes the 'ttk' module, which enhances the visual appeal and functionality of the applications. In this article, we will cover the differences, providing a
4 min read
Python | askopenfile() function in Tkinter
While working with GUI one may need to open files and read data from it or may require to write data in that particular file. One can achieve this with the help of open() function (python built-in) but one may not be able to select any required file unless provides a path to that particular file in code. With the help of GUI, you may not require to
2 min read
Python | Binding function in Tkinter
Tkinter is a GUI (Graphical User Interface) module that is widely used in desktop applications. It comes along with the Python, but you can also install it externally with the help of pip command. It provides a variety of Widget classes and functions with the help of which one can make our GUI more attractive and user-friendly in terms of both look
3 min read
Color game using Tkinter in Python
TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games. Let's try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds. Colors used in this
4 min read
Python | Message Encode-Decode using Tkinter
Prerequisite : Basics of TkinterPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Python pr
5 min read
Python - Tkinter askquestion Dialog
In Python, There Are Several Libraries for Graphical User Interface. Tkinter is one of them that is most useful. It is a standard interface. Tkinter is easy to use and provides several functions for building efficient applications. In Every Application, we need some Message to Display like "Do You Want To Close " or showing any warning or Something
4 min read
Simple registration form using Python Tkinter
Prerequisites: Tkinter Introduction, openpyxl module.Python provides the Tkinter toolkit to develop GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit. Let's make a simple information form GUI application using Tkinter. In this application, User has to fill up the required info
5 min read
Python | Simple GUI calculator using Tkinter
Prerequisite: Tkinter Introduction, lambda function Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI a
6 min read
Python | Distance-time GUI calculator using Tkinter
Prerequisites : Introduction to Tkinter | Using google distance matrix API Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest wa
7 min read
Python | Random Password Generator using Tkinter
With growing technology, everything has relied on data, and securing this data is the main concern. Passwords are meant to keep the data safe that we upload on the Internet. An easy password can be hacked easily and all personal information can be misused. In order to prevent such things and keep the data safe, it is necessary to keep our passwords
4 min read
How to visualize selection and insertion sort using Tkinter in Python?
In this article, we are going to create a GUI application that will make us visualize and understand two of the most popular sorting algorithms better, using Tkinter module. Those two sorting algorithms are selection sort and insertion sort. Selection sort and Insertion sort are the two most popular algorithms. Selection sort is a comparison-based
6 min read
Python | Real time currency converter using Tkinter
Prerequisites : Introduction to tkinter | Get the real time currency exchange ratePython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and
4 min read
Python | Real time weather detection using Tkinter
Prerequisites : Introduction to tkinter | Find current weather of any cityPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest
5 min read
Python | Simple FLAMES game using Tkinter
Prerequisites: Introduction to TkinterProgram to implement simple FLAMES game Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and e
5 min read
Python | Simple calculator using Tkinter
Prerequisite : Tkinter IntroductionPython offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Cr
3 min read
Python | 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.tt
4 min read
Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
Python Tkinter | Create LabelFrame and add widgets to it
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications. It is a widely used module which comes along with the Python. It consists of various types of widgets which can be used to make GUI more user-friendly and attractive as well as functionality can be increased. LabelFrame can be created as follows: -> i
2 min read
Python | asksaveasfile() function in Tkinter
Python provides a variety of modules with the help of which one may develop GUI (Graphical User Interface) applications. Tkinter is one of the easiest and fastest way to develop GUI applications. While working with files one may need to open files, do operations on files and after that to save file. asksaveasfile() is the function which is used to
2 min read
Geometry Method in Python Tkinter
Tkinter is a Python module that is used to develop GUI (Graphical User Interface) applications. It comes along with Python, so you do not have to install it using the pip command. 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 th
3 min read
Python | after method in Tkinter
Tkinter provides a variety of built-in functions develop interactive and featured GUI (Graphical User Interface). after() function is also a Universal function which can be used directly on the root as well as with other widgets. after(parent, ms, function = None, *args) Parameters: parent: is the object of the widget or main window whichever is us
2 min read
Python | Menu widget in Tkinter
Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself. Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipu
2 min read
Python | Create a digital clock using Tkinter
As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock using Tkinter. Prerequisites: Python functions Tkinter basics (Label Widget) Time module https://www.youtube.com/watch?v=1INA9AmaDtQ Using Label widget from Tkinter and time module: In the follow
2 min read
resizable() method in Tkinter | Python
resizable() method is used to allow Tkinter root window to change it's size according to the users need as well we can prohibit resizing of the Tkinter window. So, basically, if user wants to create a fixed size window, this method can be used. How to use: -> import tkinter -> root = Tk() -> root.resizable(height = None, width = None) Argu
2 min read
minsize() method in Tkinter | Python
In Tkinter, minsize() method is used to set the minimum size of the Tkinter window. Using this method user can set window's initialized size to its minimum size, and still be able to maximize and scale the window larger. Syntax: master.minsize(width, height) Here, height and width are in pixels. Code #1: Root window without minimum size that means
2 min read
maxsize() method in Tkinter | Python
This method is used to set the maximum size of the root window (maximum size a window can be expanded). User will still be able to shrink the size of the window to the minimum possible. Syntax : master.maxsize(height, width) Here, height and width are in pixels. Code #1: C/C++ Code # importing only those functions # which are needed from tkinter im
2 min read
Python | Loan calculator using Tkinter
Prerequisite: Tkinter Introduction Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter outputs the fastest and easiest way to create GUI applications. Creati
5 min read
Python | Add image on a Tkinter button
Tkinter is a Python module which is used to create GUI (Graphical User Interface) applications with the help of varieties of widgets and functions. Like any other GUI module it also supports images i.e you can use images in the application to make it more attractive. Image can be added with the help of PhotoImage() method. This is a Tkinter method
3 min read
destroy() method in Tkinter | Python
Tkinter supports a variety of methods to perform various tasks. It also offers some universal method. destroy() is a universal widget method i.e we can use this method with any of the available widgets as well as with the main tkinter window. Syntax: widget_object = Widget(parent, command = widget_class_object.destroy) This method can be used with
2 min read
Getting screen's height and width using Tkinter | Python
Tkinter provides some methods with the help of which we can get the current screen height and width.Following methods can be used to decide height and width : winfo_screenheight() // Returns screen height in pixels winfo_screenmmheight() // Returns screen height in mm winfo_screenwidth() // Returns screen width in pixels winfo_screenmmwidth() // Re
1 min read
Practice Tags :