DEV Community

Cover image for Build A Tkinter App (Part 1 / Basics) - Python
Corentin-zrt
Corentin-zrt

Posted on • Updated on

Build A Tkinter App (Part 1 / Basics) - Python

Today we're going to use a usefull library in python to build awesome applications. We'll use : Tkinter.

Image description

In this first part, we'll know how to create the base of our application. In a second part, we'll see the different widgets, this module propose to us and how to display them. And finally, in a third part, the inputs and actions.


The module :

First Tkinter is a module that can create some GUI applications for all plateforms as Windows, Mac OS and Linux. If you have Python already installed on your computer, you have surely tkinter with it. If it's not you can run this command in the console :

pip install tkinter
Enter fullscreen mode Exit fullscreen mode

The base of the app :

So, let's start with the script. Create a file with the .py extension (PS : you can use .pyw extension if you want to hide the console when the script is running).

In this empty file, insert the import of Tkinter :

import tkinter # Basic import
from tkinter import * # Import all propreties from tkinter module
Enter fullscreen mode Exit fullscreen mode

We'll use the second import because it's a better way to do.

Now, we can create a variable that store the application :

app = Tk()
Enter fullscreen mode Exit fullscreen mode

After that, we can asign some options to the app :

1) A title : app.title("Our Application")
2) A size by default : app.geometry("600x400") -> "widthxheight"

And now, we can finally finish the programm by this line :

app.mainloop()
Enter fullscreen mode Exit fullscreen mode

Like that our app can run on the screen.


Full script :

from tkinter import *

app = Tk()
app.title("Our Application")
app.geometry("600x400")

app.mainloop()
Enter fullscreen mode Exit fullscreen mode

Output :

Image description
Yeah, an exmpty screen... But it's a good start.


As you see, create a desktop application with tkinter is very simple. In the next post, we'll see the different widgets we can add to our app and how to display them on the screen.

If you have some questions on this first part, please write it in the comment section below.

I hope this help you. And see you soon. Bye bye 👋👨‍💻

Top comments (0)