Introduction to Python Tkinter GUI Programming

In this lesson we want to learn about Introduction to Python Tkinter GUI Programming, so Graphical User Interfaces (GUIs) are the backbone of many modern applications, from desktop software to mobile apps, and in Python Programming Language we have different options for building GUI applications, In this article we are going to explore the basics of Python Tkinter GUI programming.

 

 

What is Tkinter?

Tkinter is standard Python library for creating graphical user interfaces. TKinter provides a lot of widgets and tools for building desktop applications that can run on Windows, macOS, and Linux operating systems. Tkinter is easy to learn and it is a great choice for beginners and experienced programmers, you don’t need to install Python TKinter, because it is already installed.

 

Now let’s talk about TKinter Widgets

 

Tkinter Widgets

Tkinter provides different widgets that you can use that for building GUI applications. some of the most commonly used widgets include:

  • Button: clickable button that can perform an action.
  • Label: static text label.
  • Entry: text box that can be used to input text.
  • Frame: container for other widgets.
  • Canvas: drawing area for creating graphics.

 

Introduction to Python Tkinter GUI Programming

Now let’s create basic GUI Application with Python TKinter

In the above code we have imported the Tkinter library using alias tk. after that we have created new class called Application that extends from tk.Frame class. __init__ method initializes the frame and creates the widgets using the create_widgets method.

in create_widgets method, we creates Label widget that displays text Codeloop Programming Articles and pack it using the pack method.

and finally we have created new instance of the Application class and call the mainloop method, which starts the Tkinter event loop and keeps the application running.

 

 

If you run this code this will be the result

Introduction to Python Tkinter GUI Programming
Introduction to Python Tkinter GUI Programming

 

 

we can customize the appearance of Tkinter widgets using different options. for example we can change font and color of a label like this:

This sets font to Helvetica with a size of 24 points and the text color to red.

 

We can also change layout of widgets using grid and place methods. for example we can create a button that is centered on the screen like this:

 

 

 

This is the complete code for customizing widgets in Python TKinter

In the above example we have created four widgets label, button, text box and canvas, and also we have customized each one using different options:

label has custom font (Helvetica with a size of 24 points) and text color (red).
button has custom background color (#0077cc, a shade of blue) and text color (white).
text box has custom border (5 pixels wide with a groove style).
canvas has custom background color (white) and green rectangle drawn on it using the create_rectangle method.
By customizing these widgets, we can create more visually appealing and user friendly interface for our application.

 

 

Run your code and this is the output

Introduction to Python Tkinter GUI Programming
Introduction to Python Tkinter GUI Programming

 

 

Learn More on TKinter

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×