Building a Tkinter GUI Application from Scratch with Python
Tags: python, programming, beginners
Published: 2023-10-08 08:30:24

<p data-pm-slice="1 3 []">Tkinter is a powerful yet easy-to-use graphical user interface (GUI) toolkit that comes included with Python. It allows you to build desktop applications with Python GUIs quickly and intuitively.</p>
This step-by-step tutorial will build a complete Tkinter GUI application from scratch using the code outline provided. We'll cover key Tkinter concepts like creating windows, adding widgets, organizing layouts, configuring widgets, and responding to user interactions.

Follow along, and you'll understand how to build real-world desktop apps using Python and Tkinter GUIs.

To get started, <a class="attrlink" href="https://github.com/Scofield-Idehen/Tkinker_python.git" target="_blank" rel="noopener noreferrer nofollow">clone the repo</a> here and follow the instructions below.
<h2><strong>Key Benefits of Tkinter</strong></h2>
Let's recap some of the key advantages of Tkinter for GUI development in Python:
<ul>
 	<li>It comes included with Python, so no extra libraries are needed</li>
 	<li>Cross-platform and works on Windows, Mac, Linux etc.</li>
 	<li>Lightweight and fast, it doesn't consume many resources</li>
 	<li>It has an easy-to-use and learn API</li>
 	<li>Extensive documentation and community support</li>
 	<li>Mature and stable been developed since the early 1990s</li>
 	<li>Object-oriented design fits well with Python</li>
 	<li>Flexible layout options with pack and grid</li>
 	<li>Built-in event handling for buttons, keyboard, etc.</li>
</ul>
<h2><strong>Getting Started with Tkinter</strong></h2>
First, we import Tkinter and create the main application window:
```python
from tkinter import *

'''
the first line of code Tk() this allows us to open a graphical interface
otherwise the program closes 
'''
open_program = Tk()
```
Tk() creates the root window, which we assign to open_program. This window will be the base of our application.

Let's give the window a title using the .title() method:

<code>root.title("My Tkinter App")</code>

With that, we now have a window frame for our app - no widgets inside yet, but we'll get there soon!

<img src="https://paper-attachments.dropboxusercontent.com/s_1ED738E12D3A871A9C64094F87EE98D33156A145A61DD99D028E8D09B37C25D4_1696751341703_Screenshot+2023-10-08+at+08.48.44.png" align="left" />
<h2><strong>Placing Widgets with Pack and Grid</strong></h2>
Tkinter has two main ways to organize widgets within the window - .pack() and .grid().

.pack() stacks widgets vertically or horizontally in the window.

.grid() positions widgets in a tabular grid format by specifying rows and columns.

We'll explore both as we start adding widgets.

Let's create a simple label with .pack():
```python
message = Label(root, text="Welcome to my app!")
message.pack()
```
Run the code, and you'll see the text message displayed. Pretty easy!

Next, let's get user input with an Entry widget:

<code>e1 = Entry(open_program, textvariable=e1_soft)</code>

We can retrieve text from input later after the user types into it.
<h2><strong>Organizing with Grid</strong></h2>
Having widgets stack vertically isn't ideal for most UIs. Let's use .grid() to align the label and input side-by-side.

<code>e1.grid(row=0, column = 1)</code>

Much better! Grid allows fine-grained control over layout.

Let's add a Button using a grid too:
```python
b1 = Button(open_program, text="Execute",command = read_file )
#b1.pack() " this is one way to execute a button
b1.grid(row = 0, column = 0) #gives us more control
```
We bind our function read_file to run when clicked. And execute the command of our function, which we would create next.
<h2><strong>Responding to Events</strong></h2>
One of the most important tasks in GUIs is responding to user interactions like clicks. Tkinter makes event handling easy.

Let's print text from the input when the button is clicked:
```python
    def read_file():
    #print (e1_soft.get())
    value = float(e1_soft.get()) * 1.6
    t1.insert(END, value)
```
.get() retrieves the content of a widget, we set the value to take whatever input our user types and multiply by 1.6.

The value of our t1 would be inputted at the END and also takes the value
```python
e1_soft = StringVar()

e1 = Entry(open_program, textvariable=e1_soft)
e1.grid(row=0, column = 1)

t1 = Text(open_program, height =2, width=23)
t1.grid(row=0, column= 2)
```
This code creates GUI elements for a Python program using the Tkinter module.

Specifically:
<ul>
 	<li>e1_soft = StringVar() - Creates a Tkinter StringVar object to hold data for an Entry widget.</li>
</ul>
<ul>
 	<li>e1 = Entry(open_program, textvariable=e1_soft) - Creates an Entry widget that will display/edit the text in e1_soft.</li>
</ul>
<ul>
 	<li>e1.grid(row=0, column = 1) - Places the Entry in row 0, column 1 of the GUI layout.</li>
</ul>
<ul>
 	<li>t1 = Text(open_program, height =2, width=23) - Creates a Text widget with specified height and width.</li>
</ul>
<ul>
 	<li>t1.grid(row=0, column= 2) - Places the Text widget in row 0, column 2 of the GUI layout.</li>
</ul>
This creates an Entry box and a Text box side by side in the first row of the GUI, with the data in the Entry box bound to the e1_soft variable. The Text box is fixed in size.
<h2><strong>Final Code</strong></h2>
Our final full code so far:
```python
from tkinter import *

'''
the first line of code Tk() this allows us to open a graphical interface
otherwise the program closes 
'''
open_program = Tk()
open_program.title('Scofield_App')

def read_file():
    #print (e1_soft.get())
    value = float(e1_soft.get()) * 1.6
    t1.insert(END, value)


b1 = Button(open_program, text="Execute",command = read_file )
#b1.pack() " this is one way to execute a button
b1.grid(row = 0, column = 0) #gives us more control

e1_soft = StringVar()

e1 = Entry(open_program, textvariable=e1_soft)
e1.grid(row=0, column = 1)

t1 = Text(open_program, height =2, width=23)
t1.grid(row=0, column= 2)


open_program.mainloop()
```
We have a simple Tkinter application with widgets, layouts, and event handling.

There are many more widgets and capabilities - this serves as a good starting point for building real desktop applications with Tkinter.

In our next guide, I will discuss creating and attaching a database to our program so it stores data and retrieves data for our application.

I hope you found this step-by-step Tkinter tutorial helpful. You now have the foundation to build real desktop applications with GUIs in Python. The official Tk documentation has extensive examples for taking your skills even further.

I hope you enjoyed reading this guide and feel motivated to start your Python programming journey.

If you like my work and want to help me continue dropping content like this, buy me a <a href="https://www.buymeacoffee.com/scofields1s" target="_blank" rel="noopener noreferrer nofollow">cup of coffee</a>.

If you find this post exciting, find more exciting posts on <a class="attrlink" href="http://blog.learnhub.africa/" target="_blank" rel="noopener noreferrer nofollow">Learnhub Blog</a>; we write everything tech from <a class="attrlink" href="https://blog.learnhub.africa/category/cloud-computing/" target="_blank" rel="noopener noreferrer nofollow">Cloud computing</a> to <a class="attrlink" href="https://blog.learnhub.africa/category/frontend/" target="_blank" rel="noopener noreferrer nofollow">Frontend Dev</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/security/" target="_blank" rel="noopener noreferrer nofollow">Cybersecurity</a>, <a class="attrlink" href="https://blog.learnhub.africa/category/data-science/" target="_blank" rel="noopener noreferrer nofollow">AI</a>, and <a class="attrlink" href="https://blog.learnhub.africa/category/blockchain/" target="_blank" rel="noopener noreferrer nofollow">Blockchain</a>.
<h2><strong>Resource</strong></h2>
<ul>
 	<li><a class="attrlink" href="https://realpython.com/lessons/python-folium-get-started/" target="_blank" rel="noopener noreferrer nofollow">Getting started with Folium</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/05/26/20-essential-python-extensions-for-visual-studio-code/" target="_blank" rel="noopener noreferrer nofollow">20 Essential Python Extensions for Visual Studio Code</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/04/27/using-python-for-web-scraping-and-data-extraction/" target="_blank" rel="noopener noreferrer nofollow">Using Python for Web Scraping and Data Extraction</a></li>
 	<li><a class="attrlink" href="https://blog.learnhub.africa/2023/06/06/13-powerful-python-snippets-to-automate-tasks/" target="_blank" rel="noopener noreferrer nofollow">13 Powerful Python Snippets To Automate Tasks</a></li>
 	<li><a class="attrlink" href="https://www.python.org/about/gettingstarted/" target="_blank" rel="noopener noreferrer nofollow">Getting Started with Python</a></li>
</ul>