Last Updated On By Khizer Ali
Mostly the development is done using an IDE or command line and have the output on a terminal. Python provides solutions to add modules that allow you to integrate Graphic User Interface (GUI) in your application.
Many python GUI modules can be used. Here are the most popular modules:
In this article, Tkinter will be discussed and explored.
Tkinter is a package with python3 and you do not need to separately install it. You just need to import it.
Import tkinter
You can import multiple features like labels, buttons, widgets, etc with the import statement.
from tkinter import Tk, Label
The best part about Tkinter is that you import it, start configuring it and customize it according to your need. Here is an example of how you can use different features.
from tkinter import Tk, Label, Button
class GUI:
def __init__(self, master):
self.master = master
master.title("code leaks")
self.label = Label(master, text="let the game begin!")
self.label.pack()
self.greet_button = Button(master, text="Welcome", command=self.greet)
self.greet_button.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
def greet(self):
print("welcome !")
root = Tk()
gui = GUI(root)
root.mainloop()
Table of Contents
Here is another example of using controls without having to define a class.
import tkinter
window = tkinter.Tk()
window.title("code leaks!!")
label = tkinter.Label(window, text = "Welcome to code leaks!").pack()
button=tkinter.Button(window,text="close", command=window.quit).pack()
window.mainloop()
There are a whole lot of different controls Tkinter provides such as buttons, labels, widgets, checkboxes and way more.
1 | Button To add a button in your application. |
2 | Canvas You can draw on the canvas. |
3 | Checkbutton It provides multiple options that can be checked. |
4 | Entry A single-line text input field. |
5 | Frame Frame can contain multiple frames and organize them. |
6 | Label Provides a single-line caption and images for other widgets. |
7 | Listbox Provides a list of options to a user. |
8 | Menuebutton Displays menus in your application. |
9 | Menu Provides various commands to a user. These commands are contained inside Menubutton. |
10 | Message Displays multiline text fields for accepting values from a user. |
11 | Radiobutton Displays several options as radio buttons. |
12 | Scale Provides a slider widget. |
13 | Scrollbar Adds scrolling capability to various widgets, such as list boxes. |
14 | Text Used to display text in multiple lines. |
15 | Toplevel Provides a separate window container. |
16 | Spinbox The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values. |
17 | PanedWindow A widget that may contain any number of panes, arranged horizontally or vertically. |
18 | LabelFrame Its primary purpose is to act as a container for complex window layouts. |
19 | tkMessageBox Displays message boxes in your applications. |
Tkinter provides customization to the controls with managing the color, size, dimensions, fonts, etc. these attributes have can be conducted to be accessed by different functions to return the appropriate results.
By default, Tkinter takes the given value of lengths, widths, heights, or any other dimension as pixels. Meaning if you give a number value to the dimension it will be interpreted as pixels. However, there are other units you can use.
c | Centimeters |
i | Inches |
m | Millimeters |
Tinker takes color as a string by default. You can define colors in 2 ways
Just like CSS, you can define colors with their suitable options like background, highlightcolor, activebackground etc.
import tkFont
font = tkFont.Font (“Times”,…..,….. )
you can give multiple parameters in the tkFont calling like the following
Family − The font family name in quotations.
size − The font height as an integer in points.
weight − “bold”, “normal” weight.
slant − “italic” for italic, “roman” for straight.
underline − 1 for underlined text, 0 for normal.
overstrike − 1 for overstruck text, 0 for normal.
Python has the most simplest and easiest to modify GUI modules from which the most popular is Tkinter. You can run your application and have a customized GUI which will help the user to interact with the interface better. Tkinter is easy to import and can be instantiated, ready for customization with support of attributes.