Python GUI Development with TKinter

In this Python TKinter Tutorial we are going to learn about Python GUI Development with TKinter, we will learn that how you can create your first Python GUI window in TKinter, and also how you can work with different widgets in Python TKinter.

 

 

 

What is TKinter ?

Python is very powerful programming language. It ships with the built-in tkinter module. In only a few lines of code we can build our first Python GUI. the IDE that we are using is Pycharm Community Edition IDE. OK we have said that TKinter is a built in library in Python it means that when you have installed Python, TKinter will be installed by default so now we are going to create our first window in TKinter.

 

 

Python GUI Development with TKinter

Now let’s start our topics on building GUI applications with TKinter.

 

 

How to Create Window in TKinter

This is the code for creating window in python tkinter.

This code initializes a Tkinter application window with a specified title, minimum size and icon. tk.Tk() function creates the main application window, also win.title() sets the window title. win.minsize() defines the minimum dimensions of the window. win.iconphoto() function sets the window icon. Finally, win.mainloop() starts the Tkinter event loop, allowing the window to be displayed and interacted with.

 

 

Run the code and this will be the result

Python GUI Development with TKinter
Python GUI Development with TKinter

 

 

 

Create TKinter Window Using OOP

OK now we want to create our window using Python Object Oriented Programming, now this is the complete code.

 

This code defines custom Window class that inherits from tk.Tk, it it the main tkinter application window class. Inside the __init__ method of the Window class, window title, minimum size, and icon are set. After that an instance of the Window class is created, and the mainloop() method is called to start the Tkinter event loop, and it allows the window to be displayed and interacted with.

 

 

Run the code and this will be the result

Python GUI Development with TKinter
Python GUI Development with TKinter

 

 

 

How to Create Label in TKinter

In this section we want to learn creating of label in python tkinter, so using label you can create and add text in your tkinter window. now this is the complete code for creating of  label in python.

This code creates a tkinter window with a custom label using object-oriented programming (OOP) approach. The Root class inherits from Tk, and in its __init__ method, it sets the window title, minimum size and icon. After that it calls the create_label method to create and configure a Label widget with specified text and styling. Finally, an instance of the Root class is created, and the mainloop() method is called to start the tkinter event loop, allowing the window to be displayed and interacted with.

 

 

Run the code and this will be the result

Python GUI with TKinter - Label
Python GUI with TKinter – Label

 

 

 

How to Create Button in TKinter 

OK now let’s create button in tkinter, also we will learn how you can interact with buttons in tkinter and how you can add commands for the buttons. this is the code for creating button in tkinter.

This code creates a tkinter window with custom button and label. Window class inherits from tk.Tk, and in the  __init__ method, we set window title, minimum size and icon. After that it creates and places a Label widget and a Button widget in the window grid. Button widget is configured with a command to execute the click_me method when clicked. click_me method changes the text, foreground and background color and font of the label and also changes the text of the button. And the the end an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop, it allows the window to be displayed and interacted with.

 

 

Run the code and this will be the result

Python GUI Development with TKinter - Button
Python GUI Development with TKinter – Button

 

 

 

How to Create TextBox in Python TKinter

In this part we want to create TextBox in TKinter, this is the complete code for creating the TextBox.

This code creates a tkinter window with a textbox and a button. The Window class inherits from tk.Tk, and in the __init__ method, we set the window title, minimum size and icon. after that we calls the text_entry method to create and place a Label widget, a Textbox widget, and a Button widget in the window. Button widget is configured with a command to execute the click_me method when clicked. The click_me method updates the label text with the entered name. and lastly an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop, allowing the window to be displayed and interacted with.

 

 

 

Run the code and this will be the result

Python GUI Development with TKinter - TextBox
Python GUI Development with TKinter – TextBox

 

 

 

How to Create SpinBox in TKinter

You can use SpinBox() class for creating of spinbox in tkinter, so now this is the complete code for this section.

This code creates tkinter window with a spinbox and a scrolled text area. Window class inherits from tk.Tk, and in the __init__ method, we are setting window title, minimum size, and icon. After that we calls the spin_box method to create and place a Spinbox widget and a ScrolledText widget in the window. Spinbox widget is configured with a command to execute the spin_callback method when its value changes. spin_callback method retrieves the value from the spinbox and inserts it into the scrolled text area. And at the end an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop, allowing the window to be displayed and interacted with.

 

 

Run the complete code and this will be the result

Python TKinter SpinBox
Python TKinter SpinBox

 

 

 

How to Create RadioButton in TKinter

Now we want to create RadioButton in TKinter, you can use RadiButton class, this is the complete code for tkinter radiobutton, basically in this code we want to change the window background based on user selection value from the radio items.

 

This code creates a tkinter window with three radio buttons. Window class inherits from tk.Tk, and in in the __init__ method, we are setting window title, minimum size and icon. After that we calls the create_radio method to create and place three radio buttons in the window. Each radio button is associated with a color constant (COLOR1, COLOR2, COLOR3) and configured to call the rad_event method when selected. The rad_event method changes the window background color based on the selected radio button. And at the end an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop, and it allows the window to be displayed and interacted with.

 

 

Run the code and this will be the result

Python TKinter Radio Button
Python TKinter Radio Button

 

 

MessageBox in Python TKinter

In this part we want to create MessageBox in TKinter, there are different types of messagebox that you can use in tkinter, so we have Information, Warning and Error MessageBox. this is the complete code for MessageBox.

 

This code creates a tkinter window with three buttons, each of which displays a different type of message box (Info, Error, and Warning) when clicked. Window class inherits from tk.Tk, and in the __init__ method, we are setting window title, minimum size and icon. After that we call create_button method to create and place three buttons in the window, each associated with a different message box type. When a button is clicked, it calls the method (info_msg, warn_msg, error_msg) to display the corresponding message box using the showinfo, showwarning, and showerror functions from the messagebox module. And lastly an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop.

 

 

Run the code and this will be the result

Python TKinter MessageBox
Python TKinter MessageBox

 

 

ProgressBar in Python TKinter

In this part we want to learn creating or ProgressBar in TKinter, this is the complete code for creating of TKinter ProgressBar.

This code creates a tkinter window with a label frame containing two buttons to start and stop a progress bar, and a horizontal progress bar widget. The Window class inherits from tk.Tk, and in the __init__ method, we are seting the window title, minimum size and icon. After that we call the create_progressbar method to create and place the progress bar and buttons inside a label frame. When the “Start ProgressBar” button is clicked, it calls the start_progress method to start the progress bar animation using the start method of the progress bar widget. Similarly, when the “Stop ProgressBar” button is clicked, it calls the stop_progress method to stop the progress bar animation using the stop method of the progress bar widget. and lastly an instance of the Window class is created, and the mainloop() method is called to start the tkinter event loop.

 

 

Run the code and this will be the result

Python TKinter ProgresBar
Python TKinter ProgresBar

 

 

 

FAQs

 

Q: What is Tkinter?

A: Tkinter is the standard GUI (Graphical User Interface) toolkit for Python. It provides a set of Python bindings for the Tk GUI toolkit, which is a cross-platform graphical toolkit that comes pre-installed with most Python installations.

 

 

Q: Is Tkinter easy to learn?

A: Yes, Tkinter is generally considered to be easy to learn, especially for beginners in Python GUI development. Its simplicity and easy of use make it a popular choice for creating basic to moderately complex graphical interfaces.

 

 

 

Q: Is Tkinter suitable for complex applications?

A: While Tkinter is suitable for developing applications of different complexity, it may not be the best choice for extremely complex or highly specialized applications that require extensive customization or advanced graphics capabilities. In such cases, alternative GUI toolkits like PyQt or wxPython may be more suitable.

 

 

Q: How to create a GUI in Python with Tkinter?

A: To create a GUI in Python with Tkinter, you can follow these general steps:

    1. Import the tkinter module.
    2. Create an instance of the main Tkinter window (Tk()).
    3. Add widgets (such as buttons, labels, entry fields, etc.) to the window using Tkinter’s different widget classes(Button, Label, Entry, etc.).
    4. Configure the widgets with desired properties (text, color, size, etc.).
    5. Use layout managers (pack, grid, place) to organize the widgets within the window.
    6. Run the Tkinter event loop (mainloop()) to display the window and handle user interactions.

 

 

Q: How to learn GUI in Python?

A: To learn GUI programming in Python, especially with Tkinter, you can follow these steps:

    1. Start with basic Python programming knowledge.
    2. Familiarize yourself with the Tkinter library and its components by reading tutorials, official documentation, and books dedicated to Tkinter.
    3. Practice building simple GUI applications, start with basic widgets like buttons, labels, and entry fields, and gradually increasing complexity as you gain confidence.
    4. Experiment with different layout managers and styling options to understand how to create nice and user friendly interfaces.
    5. Explore more advanced features of Tkinter and other GUI toolkits to enhance your skills and tackle more complex projects.

 

 

 

Q: How to use Tk in Python?

A: To use Tkinter (often referred to as Tk) in Python, you need to follow these steps:

    1. Import the tkinter module in your Python script.
    2. Create an instance of the main Tkinter window using the tk() constructor.
    3. Add widgets to the window using different Tkinter widget classes (like Button, Label, Entry, Frame, etc.).
    4. Configure the widgets with desired properties and behaviors.
    5. Organize the widgets within the window using layout managers (pack, grid, place).
    6. Run the Tkinter event loop (mainloop()) to display the window and handle user interactions.

 

 

Q: Which Python GUI is best for beginners?

A: Tkinter is often considered the best GUI toolkit for beginners in Python due to its simplicity, easy of use and integration with the Python standard library. It provides a straightforward way to create graphical interfaces with minimal code, and this makes it an excellent choice for beginners learning GUI programming in Python. Also Tkinter has documentation and large community of users and contributors.

 

 

Q: Which is the best Python GUI?

A: The choice of the best Python GUI depends on different factors such as the requirements of the project, level of complexity, easy of use, platform compatibility and personal preferences. Some popular Python GUI frameworks include Tkinter, PyQt, PyGTK, Kivy and wxPython. Among these, Tkinter is often considered the best choice for beginners due to its simplicity, easy of use and integration with the Python standard library.

 

 

Q: What is Python GUI?

A: Python GUI (Graphical User Interface) refers to the visual interface that allows users to interact with a Python application using graphical elements such as windows, buttons, menus, and dialog boxes. Python provides several libraries and frameworks for building GUI applications, each with its own set of features and capabilities. GUIs in Python are used for creating desktop applications, interactive data visualization tools, games, educational software and many more.

 

 

How to install and use Tkinter in Python?

Tkinter is included with standard Python installations, so there’s no need to install it separately. 

 

 

Learn More on PyQt6:

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×