Wanna learn GUI programming with python? Tkinter is here for you.
There are many modules in python to develop desktop applications but Tkinter is the most used module to make desktop apps.
Other modules in python to make GUI :
Tkinter and these other modules make GUI programming very easy. Creating desktop applications with Tkinter is such an easy task. Let’s start GUI programming with python.
- Installing Tkinter
- Create First Desktop Application
- Tkinter Widgets
- Geometry Managers In Tkinter
- Tkinter Projects
To start with tkinter, one have to install tkinter in his/her laptop or desktop.
To install tkinter in windows machine, open command prompt and type :
pip install tkinter
It will download and install the Tkinter module for you. Now we have successfully installed Tkinter, Let’s create our first GUI using python.
Create First Desktop Application
Open your text editor or IDE whichever you are using. The first thing you need to do is import python GUI tkinter module :
import tkinter as tk
To make GUI we are using Tk class from Tkinter. So, now we need to create an instance of that class and we are assigning it to the variable win.
win = tk.Tk()
Our GUI is successfully created but when you run this script you will see nothing. We want to show our GUI continuously on the screen until the user clicks on the close button.
To do that we will have to use the method called mainloop(). mainloop() is an infinite loop used to run the application, wait for an event to occur, and process the event as long as the window is not closed.
Now we are ready to show our GUI on screen and our final code is :
#importing Tkinter module import tkinter a tk #creating instance and infinite loop win = tk.Tk() win.mainloop()
When you run this script, Output will looks like this :
Let’s move forward in GUI programming with python.
There are many widgets we can add to our Tkinter window. Like, Label, Buttons, Entry boxes, and many more…
Here are some widget table which we can use in our GUI with it’s brief introduction :
|Label||A widget used to display text on the screen|
|Button||A button can perform an action when it clicked|
|Canvas||The Canvas widget is used to draw shapes in GUI|
|Entry||The Entry widget is used to display a single-line text field for accepting |
values from a user
|Text||A text entry widget that allows multi line text entry|
|Frame||A rectangular region used to group related widgets or provide padding between widgets|
|Radio-button||The Radio button widget is used to display a number of options as radio buttons. The user can select only one option at a time.|
There are many other tkinter widgets you can add to GUI. Have a look at Tkinter Documentation
Label Widget In Tkinter
Label: A widget used to display text on the screen
We have made our basic GUI window. So, now let’s add some label text into it. To do that we have to use tk.Label() class.
Our code will looks like this : name = tk.Label(parent_window,text)
Have a look at the code.
import tkinter as tk win = tk.Tk() #Adding label greet = tk.Label(win,text="Hello, Pythons!!") greet.pack() win.mainloop()
Here our label name is label name is greet, parent window is win and lastly text is Hello, Pythons!!
Yeee, We successfully add a label to our GUI window. But we can make some changes to this label too. Like, change color, add background color, we can also change font size and font type.
Let’s do that.
Parameters we can use :
- bg – For background-color
- fg – For foreground color
- font – To change font size and type
- Many more…
We can use these parameters in widget like this,
import tkinter as tk win = tk.Tk() greet = tk.Label(win,text="Hello, Pythons!!",fg='yellow',bg='black') greet.pack() win.mainloop()
Button Widget In Tkinter
The button widget is also one of the most important in GUI. A button can perform an action when it clicked. We can add buttons in our GUI with Tkinter.
We can add button in our GUI by using tk.Button() class.
import tkinter as tk win = tk.Tk() #Button Widget a = tk.Button(win,text="This is Button") a.pack() win.mainloop()
As we saw in the label, We can also change the button color with the bg parameter. There are many other parameters you can use for the button.
import tkinter as tk win = tk.Tk() #Button Widget a = tk.Button(win,text="This is Button",bg='yellow') a.pack() win.mainloop()
Visit its official documentation for other properties, you can add in this button widget.
Entry Widget In Tkinter
Entry widget is used to take input from a user. User can add values in that entry field. We will use tk.Entry( ) for entry box.
import tkinter as tk win = tk.Tk() #Entry Widget a = tk.Entry(win,bg='yellow') a.pack() win.mainloop()
Here we just added entry box, Yellow colored box is an entry box and we put a text there (“Papa Programmer”).
To get the values from the entry box, we can use get( ) method. Like this,
entry_value = a.get()
Here we stored value in a variable called entry_value
Geometry Managers In Tkinter
Let’s discuss on layout managers or geometry managers. Those are used to give proper place to the widget in our main GUI window. Basically, there are 3 geometry managers in Tkinter.
Pack Geometry Manager In Tkinter
The pack geometry manager acts based on the concept of using up free space within the
parent widget. When packing, you can specify at which end of the free space to put the
widget, and how it will grow along with said free space.
The pack geometry manager is primarily controlled by three keyword arguments:
- side: On which end of the available space do you want to place the widget? The options are defined as constants within Tkinter, like LEFT, RIGHT, TOP, and BOTTOM.
- fill: Do you want the widget to fill any available space around it? The options are also constants: X or Y. These are Cartesian, meaning X is horizontal and Y is vertical. If you want the widget to expand in both directions, use the BOTH constant.
- expand: Should the widget resize when the window does? This argument is a Boolean, so you can pass True or 1 to make the widget grow with the window.
Grid Geometry Manager In Tkinter
The grid—as the name suggests—treats the parent widget as a grid containing rows and
columns of cells. If you are familiar with spreadsheet software, the grid will work in the
same way. The grid lines will not be visible, they are just conceptual.
To specify the position within the grid, the row and column keywords are used. These
accept integer values and begin at 0, not 1. A widget placed with grid(row=0, column=0)
will be to the left of a widget at grid(row=0, column=1). Underneath these would sit a
widget placed at grid(row=1, column=0)
Place Geometry Manager In Tkinter
Unlike pack and grid, which automatically calculate where each new widget is added,
place can be used in order to specify an exact location for a particular widget. place
takes either x and y coordinates (in pixels) to specify an exact spot, which will not
change as the window is resized, or relative arguments to its parent, allowing the
widget to move with the size of the window.
To place a widget at (5, 10) within the window, you would write widget.place(x=5, y=10)
To keep a widget in the direct center, you would use widget.place(relx=0.5, rely=0.5)
the place also takes sizing options, so to keep a widget at 50 percent width and 25 percent
height of the window, add (relwidth=0.5, relheight=0.25).
This is how you can do GUI programming with python
Here are some projects for beginners on GUI programming using python:
- Basic Calculator
- Prime Number Checker (GUI)
- Search Bar (Use webbrowser module)
- Restaurant Management System
- Tic Tac Toe or Snack Game
If you have any doubts in these projects and want more, Let me tell in comments or DM me on Instagram