In this Python graphical user interface programming tutorial, you may study a way to create GUI programs exploitation Python Tkinter toolkit.
Graphical interface, additionally referred to as graphical user interface is one in every of the simplest options in programming that creates a program look visually a lot of engaging than the traditional text-based mostly programs lined with a black or the other static colour. graphical user interface makes user interactions abundant easier and consistent.
Python graphical user interface Programming
To create a graphical user interface program in Python, you may ought to use a graphical user interface toolkit. one in every of the wide used toolkit offered in Python is Tkinter. it’s one in every of the foremost stable and widespread graphical user interface toolkit that Python developers use.
Install the Tkinter Module
Tkinter module is by default offered in Windows software system. However, if you’re exploitation the other software system, you may ought to transfer the module singly.
For Windows OS
https://www.python.org/downloads/
For UNIX OS
sudo apt-get install python-tk (For Python 2.x)
sudo apt-get install python3-tk (For Python 3.x)
You can produce graphical user interface components by instantiating objects from categories pre-defined in Tkinter module, enclosed within the Tkinter Toolkit.
Some of the graphical user interface components in the Tkinter module are listed below.
Frame: It holds alternative graphical user interface components like Label, Button, TextBox, etc.
Label: It displays associate degree uneditable text or icons on the screen layout.
Button: It performs associate degree action once the user activates it or presses it exploitation mouse.
Text Entry: It accepts one line of text and displays it.
Text Box: This graphical user interface component accepts multiple lines of text and displays it.
Check Button: It permits the user to pick out or unselect associate degree choice.
Radio Button: This button provides the user to pick out one choice from many choices listed.
GUI programs are historically event-driven. Event-driven implies that the Buttons, Icons or the other graphical object on the screen reply to actions in spite of the order within which they occur. They reply to the actions performed by the user and not the logical flow as we have a tendency to saw in text-based mostly programming before.
Python graphical user interface Programming Example
from Tkinter import *
root= Tk()
root.title("My First GUI")
root.geometry("500x500")
root.mainloop()
Output
My First GUI
The first line of the program code imports the Tkinter module from the Python library within the namespace of this program.
The second statement is employed to instantiate associate degree object of the Tkinter category Tk and it’s assigned to a user-outlined variable root.
The third statement uses the title() methodology that describes the title to be displayed on to the window title bar. It takes in a very strong argument.
The third statement uses a geometry(a) methodology that is employed to line the size of the window. It takes in a very strong (and not integers) as arguments that
represents the window’s breadth and height, separated by the “x” character.
Next statement finally starts up the graphical user interface window application and waits surely the user to command in.
Note: you’ll solely have one root window in a very Tkinter program. If you are attempting to form multiple windows in one single program, your program is absolute to crash.
Python Tkinter Frame
A-Frame could be a device or a base widget that is employed to a position in alternative widget like labels, text boxes et al. This is a crucial start as nothing may be done unless frames are developed. It essentially holds alternative widgets.
app=Frame(root)
Here, we’ve passed root to the Frame creator. As a result, the new frame is placed within the foundation window.
app.grid()
grid() methodology is that the one that each one widget have. It’s related to a layout manager, that helps you to organize widgets in a very Frame.
Python Tkinter Label
GUI components are referred to as widgets. The label is one in every of the only device. It consists of uneditable text or icons (or both). It’s typically wont to label alternative widgets. Labels aren’t interactive. You won’t generate any command by clicking on a label. But, labels are vital for naming alternative widgets so there’s no confusion for the top user.
Example
from Tkinter import *
root= Tk()
root.title("My First GUI")
root.geometry("800x200")
frame1=Frame(root)
frame1.grid()
label1 = Label(frame1, text = "Here is The label!")
label1.grid()
root.mainloop()
Output
My First GUI
Here Is The lable
Here, we’ve 1st created a Frame that is assigned to variable frame1. we’ve then passed frame1 to the label1 object’s creator, and thereby the frame that app refers to the master of the label device. As a result, the label is placed within the frame.
In this next Python graphical user interface Programming tutorial, we’ll discuss regarding a lot of GUI components or widgets.