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:
- Tkinter: it is found along with the Python bundle using TK and is the standard framework of Python.
- WxPython: this was first known as WxWindows but now it has been recognized as WxWidgets library. WXPython is an open source for cross-platform GUI library.
- PyGUI: is the simplest and light-weighted due to the API being synched with python completely.
- JPython: Java port which allow python scripts to give access to the libraries that can be used in python.
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.
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()
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.
To add a button in your application.
You can draw on the canvas.
It provides multiple options that can be checked.
A single-line text input field.
Frame can contain multiple frames and organize them.
Provides a single-line caption and images for other widgets.
Provides a list of options to a user.
Displays menus in your application.
Provides various commands to a user. These commands are contained inside Menubutton.
Displays multiline text fields for accepting values from a user.
Displays several options as radio buttons.
Provides a slider widget.
Adds scrolling capability to various widgets, such as list boxes.
Used to display text in multiple lines.
Provides a separate window container.
The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.
A widget that may contain any number of panes, arranged horizontally or vertically.
Its primary purpose is to act as a container for complex window layouts.
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.
Tinker takes color as a string by default. You can define colors in 2 ways
- Local name of the color. (eg. Black, blue)J
Just like CSS, you can define colors with their suitable options like background, highlightcolor, activebackground etc.
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.