GUI (Graphical User Interface)
GUI is a graphics-based operating system that uses icons and menus to interact with the user. Python mostly works on CLI (Command Line Interface).
Any user interface has a lot of components. Its layout consists of many small or large components, and these components are collectively known as widgets. There can be multiple common and custom widgets in any GUI developing toolkit. While performing any python programming for GUI development, a programmer will take all these widgets and arrange them to create the desired layout.
A GUI (graphical user interface) works when a user does something. That something is known as an event. An event happens when a user does something, presses the mouse, or clicks any button on the keyboard. Any GUI toolkit runs an infinite event loop that will track these events and perform the respective activity.
Python provides many different options for developing GUI (Graphical User Interface). Some of those developing kits are given below:
There are a lot of other developing kits. Let’s understand some in detail.
Out of all the other developing kits, Tkinter is the most used and famous one. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Using Python in Tkinter is the fastest and easiest way to create GUI applications. It is a very easy task to create GUI using Tkinter. It was written by Fredrik Lundh. Tk provides some of the following features or widgets:
- Frame: This widget is used as a container for storing or fitting other widgets.
- Label: This widget is used to provide labels to other widgets.
- Check-button: This widget displays several checkboxes for a certain number of options.
- Entry: This widget is used to accept one-line input from users.
- Lebel-frame: this widget provides a container for complex structure application layouts.
- Message: This widget is used for displaying multiline text fields to accept user input values.
- Button: This widget will display a button in your application.
- Canvas: This widget is used to draw shapes in your application, like rectangles, circles, ovals, polygons, etc.
Creating any GUI program using Tkinter is a very easy task. We can follow the following steps below to do so:
- Import the module Tkinter.
- Create the GUI application main window.
- Add any number of widgets mentioned above to the GUI application.
- Enter the main event loop to take action against each event triggered by the user.
import tkinter val = tkinter.Tk() #widgets are added here val.mainloop()
There are two main methods we should remember before starting a python program with GUI, which are
- Tk(screenName=None, baseName=None, className=’Tk’, useTk=1)
This is used to create the window size of your application. You can change the name of your window from Tk to anything else by changing the value in className.
This is used to run the application. This method is called when your application is ready to run. It is an infinite loop.
Standard Attributes for GUI
wxPython is another kit for developing python GUI. It was developed in 1998. The main difference between the wxPython toolkit and any other toolkit is that wxPython uses actual widgets on native platforms, which makes its application more native to the Os on which it is running. It supports custom widgets which is a very interesting feature in this toolkit. Along with millions of widgets, it also includes custom widgets.
pip install wxpython
import wx app = wx.App() frame = wx.Frame(parent=None, title='Hello World') frame.Show() app.MainLoop()
In the first line of the above code, we have imported wxPython as wx. After that, we created an application using a python object name, which we used later as wx.App. In the third line, we have created the frame or basic layout of the application. In this frame, we have given the application name ‘Hello World’, but we can change it to anything we want. After that, we used the function Show() to show the application, and when our application is ready to run, we will use the mainloop() method.
In the above tutorial, we have learned the basics of how we can create applications using python programming and what toolkits we can use for creating a GUI. There is a lot more content available on the web to understand these concepts in-depth, so if you are starting to learn more about this concept, you can explore videos and courses available online.