Introduction:
This post is all about how to get started with GUI programming with Python and how to use Tkinter framework of Python to make exceptional GUI tools for windows, mac and linux devices. By the end of this long post(Not too long though๐ ), you can at get started with Tkinter and understand what it is all about. Consider it as an introduction to GUI Development and to Tkinter. Let's get started now.
What is Tkinter?
Ok, so basically Tkinter is a framework of Python that come bundled with the Python language and can be used without installing any 3rd party dependencies. It is used to make GUI applications in Python and has native support for making desktop applications of Windows, Linux, and MacOS. Since it is present in the python package natively, it is the most popular framework for making GUI applications in Python.
How to get started with Tkinter?
To start making GUI apps, we first have to import the Tkinter module in our program environment. This is how we can get it done: import tkinter as tk
.
This way we can get the module in our program to be used. Let's start a window with our tkinter framework.
Making a window object with Tkinter:
To make a window object, we have to use the Tk() function of the tkinter module. If we want to make a window with the name window, we can do it like this: window = tk.Tk()
. Since I have imported the module by the name tk, I can use this method easily, but this can also be done this way: window = tkinter.Tk()
.
After doing all this we would (if not for any bugs!๐) get a window if we would run the code. Although if at this point you would run the code, the code would execute perfectly without showing any window, this is not a bug, the window did open but it instantly closes. You can say that it does not get the time to show up in front you๐ .
To keep the window open, we would need the mainloop()
function of the window object.This is how we can do it: window.mainloop()
. The mainloop function keeps our window open and thus we can use it for making our GUI.
This is the code so far:
import tkinter as tk
# Used to make the window object that would contain all the function necessary to make GUI.
window = tk.Tk()
# Would keep the window open and prevent it from closing until we do it.
window.mainloop()
If, at this point you would run the code, this is what you are going to see.
A completely blank screen which was not there until we run the code. This is the screen which we made. Congratulations on making your first ever GUI screen๐๐๐. From now on we would meddle with this screen to see what more we can do with it.
What can the window object do?
So, as I have previously said that the window object is like the playground where all the stuff we make as GUI would exist. That being said, we can still make a few changes to our window object, it has some amazing function with which, we can change it's size, color, title or even set the minimum and maximum size of the window. Let's have a look at each one of them.
Title:
To change the title of our window we can use the title() function. It takes a string as argument and then use the string as the title of the window. This is how it is done window.title("title of the window")
Background Color:
We can also change the background color of the window. The background color of the window is like a dictionary in the window object, thus it is to be changed the same we change the value of a key-value pair in Python. This is how it is done: window['bg'] = "green"
, this line would set the background color as green.
Dimensions:
To open the window in fullscreen mode by default, we can achieve this by changing the state to zoomed. This is how it is done: window.state("zoomed")
. Now, the window would open in the fullscreen mode by default. By this point, this is how our codebase looks like:
import tkinter as tk
# Used to make the window object that would contain all the function necessary to make GUI.
window = tk.Tk()
# Sets the title of the window.
window.title("GUI with Python")
# Sets the background color to green
window['bg'] = "Green"
# Opens the window in fullscreen by default.
window.state("zoomed")
# Would keep the window open and prevent it from closing until we do it.
window.mainloop()
I would appreciate if you would take the time to meddle with the code and execute it on your own system. Trust me, it would be worth it๐.
I hope you liked this little tutorial which I made on Tkinter, although I say it was on tkinter, it is more like the start of a series in which I would everyday post an article about tkinter, thus I thought it was best to start it with nothing but the building block of whole thing GUI, the window object.
In the next one, I am gonna post about what are widgets and how to use widgets to make your application unique and great. I hope you enjoyed this little tutorial (although it was pretty boring tbh๐ ).
If you followed this far in the tutorial, I would ask of you to follow me and subscribe to my newsletter so that you never miss any article that I post about Python and Tkinter. Hope you to have a great day๐.
Top comments (0)