To make any project user-friendly you must have an Application whether it is a Web app or a PC App. In this tutorial, we will learn how to create desktop application using python.
Can python be used for desktop applications? Yes, there are various libraries in Python, but Tkinter is one of the simplest. So, we will be using this library to develop a simple desktop application in python.
Before we start building a desktop application I am assuming you have some basic knowledge of Python. Because in this tutorial I am not going to cover the basics of python.
In this tutorial, I am going to use Tkinter library to create a desktop application using python. But Tkinter is not the only library. Other GUI frameworks are available in Python to create applications like:
Tkinter is my preferred framework because it is simple and lightweight. It is a simple framework that is best suited for beginners. However, if you want to explore more, you can learn about other frameworks.
A Simple Desktop Application in Python
Okay now, let’s try to develop a simple desktop application using the Tkinter library of Python.
What do we want to achieve with this PC App?
Before starting coding let’s understand what we want to achieve from this windows App. I mean what kind of functionality our App should handle?
- I want the Application to handle two buttons
- If we click a button it should show which button we clicked in text format
I believe, to learn any framework to build an application (whether it is a web app or desktop app), if you know how to handle button activity, you almost learned 90% basics of that framework.
With that being said let’s now make a simple windows software using python. Let me break the entire code into some steps to explain better.
Step1: Import Library
In order to use a library in Python, we must import it into our code. So, let’s import
tk. Why do we import
tk? It is for your convenience so that we can use
tk instead of
Tkinter whenever it is required.
import tkinter as tk
Step2: Create Software Window
Next, we need to create a window for our App. This can be done by using the
Tk() method of
Tkinter object. Let’s do that.
# Create window Tkinter window = tk.Tk()
Step3: Naming title to the window
We now have a window for our desktop app. Let’s give this window a name. We can accomplish this by using the
title() method of Tkinter. I am giving the title of my demo application as “Simple Windows App”.
window.title(" Simple Windows App ")
Step4: Define Window size in Tkinter
Let’s now define the geometry of our application window. This is the screen size of our app window. We can define our app window size using
geometry() method of Tkinter.
Now there are four ways to define screen window size in Tkinter.
Method 1: Default Window size
If you are going with the default configuration, you no need to specify geometry. In that case, when you run the application, you’ll notice that the Tkinter window appears in the top-left position of the screen and its size is small.
Method 2: Fixed Window Size
In this configuration, you can define a fixed size of your app window. To do this you just need to pass desired pixel values into the
geometry() function. In this case, I am using width as 700 and height as 500 pixels.
Method 3: Full-Screen Window using attributes() function
This type of configuration will allow you to create a full-screen window in Tkinter for creating GUI applications in Python. To do that we need to use the
attributes() method of Tkinter and set the parameter
‘-fullscreen’ and True for setting the size of our window to fullscreen and to False otherwise.
Disadvantage: It will show an output Tkinter WINDOW with no toolbar. Thus you can not select any other software. This disadvantage can be solved by the next method.
Method 4: Tkinter Window Screen above the Toolbar
By this technique, we can get a Tkinter output window with the toolbar above.
width = window.winfo_screenwidth() height = window.winfo_screenheight() window.geometry("%dx%d" % (width, height))
Method 5: Maximized Window using state() function
By this method, you can maximize the window and make it fullscreen. To do that, we need to use
state() function of Tkinter python and pass
'zoomed' parameter string into it.
Both method 4 and method 5 are almost same. In method 4 you can observe the window is not completely fullscreen, there is some gap. On the other hand, using
state() method you will get a truly fullscreen window above the toolbar.
I hope at this point you are clear about which one to choose for your application. For this tutorial, I am going to use fixed window using
geometry() the method. Okay, now let’s move on to the next step to build a desktop application in python.
Step5: Create a Label in Tkinter
As I said our application should display which button is clicked. To display text we need to add a label widget in our application. The below code is to do that.
# Create a label widget in Tkinter label = tk.Label(window, text="Click the Button to update this Text", font=('Calibri 15 bold')) label.pack(pady=20)
Here in this code:
paddyis nothing but padding. The number of pixels to pad (distance) widget, horizontally and vertically. Basically, this represents the position of
labelin our software window.
Now there are lots of other fonts available you can choose for text labels in Tkinter. To get the list of fonts in Tkinter run the below code.
# python font name list for Tkinter application from tkinter import Tk, font root = Tk() font.families()
'System', '8514oem', 'Fixedsys', 'Terminal', 'Modern', 'Roman', 'Script', 'Courier', 'MS Serif', 'MS Sans Serif'
Step6: Create a function to update Lable text
In this step, we need to define two functions for two buttons. If we click the 1st button it should display “You clicked first button” and same for the 2nd button “You clicked second button“
# Function to update the label text for first button click in Tkinter def on_click_btn1(): label["text"] = "You clicked first button" # Function to update the label text for second button click in Tkinter def on_click_btn2(): label["text"] = "You clicked second button"
Step6: Create Button Activity
In this step, we will add to buttons into our application window and define activity of each button.
# Create 1st button to update the label widget btn1 = tk.Button(window, text="Button1", command=on_click_btn1) btn1.pack(pady=20) # Create 2nd button to update the label widget btn2 = tk.Button(window, text="Button2", command=on_click_btn2) btn2.pack(pady=40)
Here in this code, we are passing the corresponding onclick function (
on_click_btn2) to each button as a
Step7: Run mainloop()
At this point we are done with our application, we just need to run the
mainloop() method to see the application live. It is the main program that will execute everything that we have written above. So let’s run it.
That’s it. You successfully created a Desktop Application Using Python which should be up and running in your windows system right now.
I spoke about lots of things to just make a simple desktop application in Python. Let me now consolidate everything and make a final code for you.
# ---------Full code to create a software application with python --------------- # Import library import tkinter as tk # Create window Tkinter window = tk.Tk() # Name our Tkinter application title window.title(" Simple Windows App ") # Define window size in Tkinter python window.geometry("700x500") # Create a label widget in Tkinter label = tk.Label(window, text="Click the Button to update this Text", font=('Calibri 15 bold')) label.pack(pady=20) # Function to update the label text for first button click in Tkinter def on_click_btn1(): label["text"] = "You clicked first button" # Function to update the label text for second button click in Tkinter def on_click_btn2(): label["text"] = "You clicked second button" # Create 1st button to update the label widget btn1 = tk.Button(window, text="Button1", command=on_click_btn1) btn1.pack(pady=20) # Create 2nd button to update the label widget btn2 = tk.Button(window, text="Button2", command=on_click_btn2) btn2.pack(pady=40) # Run main loop window.mainloop()
Making desktop applications is simple, especially when you code in Python. Tkinter can also be used to create advanced pc apps that can be useful in real-world situations.
Some Simple Web App Python Projects You may also read:
I hope this article was helpful to learn how to create desktop application using python. If you have any questions or suggestions regarding this tutorial, let me know those in the comment section below.
Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.
2 thoughts on “How to Create Desktop Application Using Python”
This is very easy and understandable button program. Thanks Naskar
Thank you sir.
I learned my 1st step of making PC app.