April 6, 2020

Explain Python GUI programming?

Python GUI:

Python is a general-purpose, dynamic, high-level programming language. It supports an object-oriented programming approach to develop applications. It is simple and easy to learn programming language and provides more high-level data structures.

Python GUI is a desktop app which provides an interface. This interface helps you to interact with the computers as well as gives command-line input to your code. They used to perform different tasks in desktops, laptops, and other electronic devices, etc.

Applications of Python GUI :
It is used for creating a calculator that has a user-interface and functionalities.
Text-Editors, IDE's for coding using GUI app.
GUI apps like, sudoku, chess, solitaire, etc..
The Chrome, Firebox, Microsoft Edge are using a GUI app to browse the internet.

If You are interested to Learn Python you can enroll for free live demo Python Online Training

Frameworks to develop Python GUI:
Python provides some of the frameworks to develop GUI are given below
.
Jython:
It is the python platform for java. It provides Python scripts that logically access the java library for the local machine.

wxPython:
It is an open-source as well as cross-platform GUI toolkit i.e written in c++. With the help of this, we can create native applications for Windows, Mac OS, and Unix.

Tkinter:
It is a python standard GUI package. It is a mostly used toolkit for GUI programming. It is simple as well as open-source i.e available at the python Licence.
Python provides the standard library Tkinter for creating the GUI for desktop applications.
Now, let us build the GUI with the help of Python Tkinter. It will understand with the help of the following diagram.

Developing desktop applications with python Tkinter is simple. This Tkinter can be created with the help of following steps:
1. First, import the Tkinter module.
2. Then create the main application window.
3. Now add the widgets such as labels, buttons, frames, etc. to the window
4. Finally, call the main event loop. So that actions are displayed on the user's screen.
The following code is used to creating an application
# !/usr/bin/python3
from tkinter import *
#creating the application main window.
top = Tk()
#Entering the event main loop
top.mainloop()

Tkinter widgets:
The below widgets are using to build python GUI applications.
Button:
This is using to add various types of buttons to the python applications.
Canvas: This can use to draw the canvas on the window.
Check button:
It uses to show the CheckButton in the window.
Entry:
This widget is used to display the single-line arear to the user. It is mostly used to accept user values.
Frame:
It is defined as a container. By using this we can add another widget.
Label:
It is a text used to display some message or information on the other widgets.
ListBox:
This uses to display a list of options to the user.
Menubutton:
By using this we can display menu items to the user.
Menu:
It is using to add menu items for the user.
Message:
This is used to display the message box to the user.
Radiobutton:
It provides various options and the user can select only one option among them.
MessageBox:
This module is used to display message-box in desktop applications.

Python Tkinter geometry:
The Python Tkinter geometry specifies the method, i.e used to display widgets. This provides the following geometry methods.
1. The pack() method:
This is used to organize the widget in the block. The positions of the widgets added to the python application using this method. This can be controlled with the help of various options that are specified in the method call.

The syntax for Pack() is given below.
widget.pack(options)

Pack() contains a list of possible options that are given below.
Expand: If this is set to true, then the expands to fill any space.
Fill: Default, this set to NONE. So, we can set it has some names to determine any extra space in the widget.

Size: It represents the size of the widget.
The following shows code about Python tkinter pack()
# !/usr/bin/python3
from tkinter import *
parent = Tk()
redbutton = Button(parent, text = "Red", fg = "red")
redbutton.pack( side = LEFT)
greenbutton = Button(parent, text = "Black", fg = "black")
greenbutton.pack( side = RIGHT )
bluebutton = Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = TOP )
blackbutton = Button(parent, text = "Green", fg = "red")
blackbutton.pack( side = BOTTOM)
parent.mainloop()
Output:

2.The grid() method:

This geometry method displays the widgets in the tabular form. This use to specifying the rows and columns as the options in the method call. As well as it specifies the width and height of the widget.
Syntax used for grid() is
widget.grid(options)
The following options are using in the grid(). They are
Column:
This is used to provide a column number for the widget. The left-most column is identified by 0.

Columnspan:
It represents the width of the widget. It shows the number of columns to expanded columns.
Row:
It is used to represent the row number for the placed widget. The top row is represented by 0.
rowspan:
This represents the height of the widget, i.e number of up row to the expanded widget.

The given below represents the Grid() widget.
# !/usr/bin/python3
from tkinter import *
parent = Tk()
name = Label(parent,text = "Name").grid(row = 0, column = 0)
e1 = Entry(parent).grid(row = 0, column = 1)
password = Label(parent,text = "Password").grid(row = 1, column = 0)
e2 = Entry(parent).grid(row = 1, column = 1)
submit = Button(parent, text = "Submit").grid(row = 4, column = 0)
parent.mainloop()
Output:

3.The place() method:
The place() geometry provides the widgets to the specific coordinates.

The Syntax for place() method is
widget.place(options)

The following options that are involved in place() are:

Anchor:
It shows the exact position of the widget within the container.
bordermode:
The default value for this type is INSIDE. It refers ignore the parents inside the border.
height, width:
It shows the height and width in pixels.

The following code shows about palce()
# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("400x250")
name = Label(top, text = "Name").place(x = 30,y = 50)
email = Label(top, text = "Email").place(x = 30, y = 90)
password = Label(top, text = "Password").place(x = 30, y = 130)
e1 = Entry(top).place(x = 80, y = 50)
e2 = Entry(top).place(x = 80, y = 90)
e3 = Entry(top).place(x = 95, y = 130)
top.mainloop()

Output:

In this article, I have explained about Python GUI and Tkint framework to build GUI applications. I hope this article gave Awareness about Python GUI.