Seven Python GUI libraries, which can't be done after learning! Very easy to use!


GUI (graphical user interface), as the name suggests, is to display the computer operation interface in a graphical way, which is more convenient and intuitive.

The corresponding is Cui (command line user interaction), which is a common Dos command line operation. You need to remember some common commands. For ordinary people, it is very difficult to learn.

A good-looking and easy-to-use GUI can greatly improve everyone's use experience and efficiency.

For example, if you want to develop a calculator, there is no user experience if it is just a program input and output window.

Therefore, it becomes necessary to develop an image-based small window.

Today, little F will introduce you to seven necessary GUI libraries for Python, each of which is worth learning.

01. PyQt5

PyQt5 was developed by Riverbank Computing. Built based on Qt framework, it is a cross platform framework that can create applications for various platforms, including Unix, Windows, Mac OS.

PyQt combines Qt with Python. It's not just a GUI toolkit. It also includes threads, Unicode, regular expressions, SQL databases, SVG, OpenGL, XML and fully functional Web browsers, as well as a rich collection of GUI widgets.

Use pip to install it.

# Install PyQt5
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5

After the installation is successful, give a simple example of Hello Word.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout

# Create application object
app = QApplication(sys.argv)
# Create form object
w = QWidget()
# Set form size
w.resize(500, 500)

# Set style
w.layout = QVBoxLayout()
w.label = QLabel("Hello World!")
w.label.setStyleSheet("font-size:25px;margin-left:155px;")
w.setWindowTitle("PyQt5 window")
w.layout.addWidget(w.label)
w.setLayout(w.layout)

# Display Form 
w.show()
# Run program
sys.exit(app.exec_())

The results are as follows.

Document address

Tutorial links

02. Tkinter

Tkinter is one of the most popular GUI libraries in Python. Because of its simple syntax, it has become one of the first choices for beginners of GUI development.

Tkinter provides various widgets, such as labels, buttons, text fields, check boxes and scroll buttons.

Grid layout is supported. Since most of our programs are rectangular display, it becomes easier to develop even complex designs.

# Installing tkinter
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tkinter

Next, use Tkinter to design a BMI calculator.

Take weight and height as input, and return BMI coefficient as output in the pop-up box.

from tkinter import *
from tkinter import messagebox

def get_height():
    # Get height data (cm)
    height = float(ENTRY2.get())
    return height

def get_weight():
    # Obtain weight data (kg)
    weight = float(ENTRY1.get())
    return weight

def calculate_bmi():
    # Calculate BMI coefficient
    try:
        height = get_height()
        weight = get_weight()
        height = height / 100.0
        bmi = weight / (height ** 2)
    except ZeroDivisionError:
        messagebox.showinfo("Tips", "Please enter valid height data!!")
    except ValueError:
        messagebox.showinfo("Tips", "Please enter valid data!")
    else:
        messagebox.showinfo("Yours BMI The coefficient is: ", bmi)

if __name__ == '__main__':
    # Instantiate the object and create a window TOP
    TOP = Tk()
    TOP.bind("<Return>", calculate_bmi)
    # Set the size of the window (length * width)
    TOP.geometry("400x400")
    # Window background color
    TOP.configure(background="#8c52ff")
    # Window title
    #Python learning skirt 872937351
    TOP.title("BMI Calculator")
    TOP.resizable(width=False, height=False)
    LABLE = Label(TOP, bg="#8c52ff", fg="#ffffff", text =" welcome to BMI calculator ", font =" Helvetica ", 15," bold ", paddy = 10)
    LABLE.place(x=55, y=0)
    LABLE1 = Label(TOP, bg="#ffffff", text =" enter weight (unit: kg):", bd=6,
                   font=("Helvetica", 10, "bold"), pady=5)
    LABLE1.place(x=55, y=60)
    ENTRY1 = Entry(TOP, bd=8, width=10, font="Roboto 11")
    ENTRY1.place(x=240, y=60)
    LABLE2 = Label(TOP, bg="#ffffff", text =" enter height (unit: cm):", bd=6,
                   font=("Helvetica", 10, "bold"), pady=5)
    LABLE2.place(x=55, y=121)
    ENTRY2 = Entry(TOP, bd=8, width=10, font="Roboto 11")
    ENTRY2.place(x=240, y=121)
    BUTTON = Button(bg="#000000", fg='#ffffff', bd=12, text="BMI", padx=33, pady=10, command=calculate_bmi,
                    font=("Helvetica", 20, "bold"))
    BUTTON.grid(row=5, column=0, sticky=W)
    BUTTON.place(x=115, y=250)
    TOP.mainloop()

The interface is as follows
When there is no data, click the BMI button and there will be a corresponding prompt.

Let's use the correct data to see the results.
It feels good to use.

03. Kivy

Kivy is another open source Python library. Its biggest advantage is that it can quickly write mobile applications (mobile phones).

Kivy can run on different platforms, including Windows, Mac OS, Linux, Android, iOS and raspberry pie.

In addition, it is also free to use and has obtained MIT license.

# Install kivy
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple kivy

A Hello World window based on Kivy.

from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text=" Hello Kivy World ")

TestApp().run()

give the result as follows

04. wxPython

Wxpthon is a Python Library of cross platform GUI, which can easily create powerful and stable GUI. After all, it is written in C + +

Currently, Windows, Mac OS X, Mac OS and Linux are supported.

Applications (GUIs) created using wxPython have a native look and feel on all platforms.

# Install wxPython
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple wxPython

Next, create a basic GUI example using wxPython.

import wx

myapp = wx.App()
init_frame = wx.Frame(parent=None, title='WxPython window')

init_frame.Show()
myapp.MainLoop()

give the result as follows

Document link

05. PySimpleGUI

PySimpleGUI is also a Python based GUI framework. You can easily make custom GUIs.

The four most popular GUI frameworks QT, Tkinter, wxpthon and Remi are adopted, which can realize most of the sample code and reduce the difficulty of learning.

Remi converts the application's interface to HTML for rendering in a Web browser.

# Install PySimpleGUI
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PySimpleGUI

The following is a simple case.

import PySimpleGUI as sg

layout = [[sg.Text("test PySimpleGUI")], [sg.Button("OK")]]
window = sg.Window("Sample", layout)
while True:
    event, values = window.read()
    if event == "OK" or event == sg.WIN_CLOSED:
        break
window.close()

give the result as follows
Click the OK button and the window disappears.

06. PyGUI

PyGUI is a GUI framework famous for its simple API, which reduces the amount of code between Python applications and the underlying GUI of the platform.

Lightweight API can make your application run more smoothly and faster.

It also has open source code and cross platform projects. At present, it can run on Unix based systems, Windows and Mac OS.

Both Python 2 and python 3 can be supported.

Document address

Tutorial links

07. Pyforms

Pyforms is a cross platform framework for developing GUI applications.

Pyforms is a python 2.7 / 3. X cross environment graphics application development framework. Modularization and code reuse can save a lot of work.

Allows applications to run on the desktop, Web and terminal without modifying the code.

# Install PyFroms
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyFroms

Document address

Tags: Python GUI

Posted on Mon, 11 Oct 2021 18:28:16 -0400 by msgcrap