In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Python essential GUI library, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
GUI (graphical user interface), as the name implies, is a graphical way to display the interface of computer operation, which is more convenient and intuitive.
The corresponding is CUI (Command Line user interaction), which is a common Dos command line operation, which needs to remember some commonly used commands. For ordinary people, it is quite difficult to learn.
A good-looking and easy-to-use GUI can greatly improve everyone's experience and efficiency.
For example, if you want to develop a calculator, if it is only a program input and output window, there is no user experience.
Therefore, it is necessary to develop a small graphic window.
Today I will introduce to you seven essential GUI libraries for Python, each of which is worth learning.
01. PyQt5
PyQt5 is developed by Riverbank Computing. Based on the Qt framework, it is a cross-platform framework that can create applications for a variety of 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 a full-featured Web browser, as well as a rich collection of GUI widgets.
Install it using pip.
# 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 # build application object app = QApplication (sys.argv) # build 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:
Https://riverbankcomputing.com/software/pyqt/intro
Tutorial links:
Https://www.guru99.com/pyqt-tutorial.html
02. Tkinter
Tkinter is one of the most popular GUI libraries in Python. Because of its easy to learn grammar, it has become one of the first choices for beginners in GUI development.
Tkinter provides a variety of widgets, such as labels, buttons, text fields, check boxes and scroll buttons.
Support Grid (grid) layout, because most of our programs are rectangular display, which makes it easier to develop even complex designs.
# install tkinter pip install-I https://pypi.tuna.tsinghua.edu.cn/simple tkinter
Let's use Tkinter to design a BMI calculator.
Take weight and height as inputs and return the 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 (): # get 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 ("prompt" "Please enter valid height data!" Except ValueError: messagebox.showinfo ("prompt", "Please enter valid data!") Else: messagebox.showinfo ("your BMI coefficient is:", bmi) if _ _ name__ = ='_ main__': # instantiate object Create window TOP TOP = Tk () TOP.bind (", calculate_bmi) # set window size (length * width) TOP.geometry (" 400x400 ") # window background color TOP.configure (background=" # 8c52ff ") # window title 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"), pady=10) LABLE.place (x = TOP 55, y = 0) LABLE1 = Label (TOP, bg= "# ffffff", text= "input weight (in kg):", bd=6, font= ("Helvetica", 10, "bold"), pady=5) LABLE1.place (x = Entry (TOP, bd=8, width=10, font= "Roboto 11") ENTRY1.place (x = 240) 60) LABLE2 = Label (TOP, bg= "# ffffff", text= "enter height (in cm):", bd=6, font= ("Helvetica", 10, "bold"), pady=5) LABLE2.place (x = Entry (TOP, bd=8, width=10, font= "Roboto 11") ENTRY2.place (x = Button 240,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 (xylene 115, yearly 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, the biggest advantage is that you 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 used free of charge and licensed by MIT.
# install kivy pip install-I https://pypi.tuna.tsinghua.edu.cn/simple kivy
A Kivy-based Hello World window.
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 ()
The results are as follows.
04. WxPython
WxPython is a cross-platform GUI Python library, which can easily create powerful and stable GUI. After all, it is written in C++.
Currently, Windows,Mac OS Xmai MacOS and Linux are supported.
Applications (GUI) created with wxPython have a native look and feel on all platforms.
# install wxPython pip install-I https://pypi.tuna.tsinghua.edu.cn/simple wxPython
Let's use wxPython to create a basic GUI example.
Import wx myapp = wx.App () init_frame = wx.Frame (parent=None, title='WxPython window') init_frame.Show () myapp.MainLoop ()
The results are as follows.
Document link: https://www.wxpython.org/
05. PySimpleGUI
PySimpleGUI is also a GUI framework based on Python. You can easily make custom GUI.
The four most popular GUI frameworks, QT, Tkinter, WxPython and Remi, are used to implement most of the sample code and reduce the difficulty of learning.
Remi converts the application's interface to HTML for rendering in Web browsers.
# install PySimpleGUI pip install-I https://pypi.tuna.tsinghua.edu.cn/simple PySimpleGUI
Here is a simple case study.
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 ()
The results are as follows.
Click the OK button and the window disappears.
06. PyGUI
PyGUI is a GUI framework known 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 smoother and faster.
At the same time also open source code, cross-platform projects. It currently runs on Unix-based systems, Windows and Mac OS.
Both Python2 and Python3 can be supported.
Document address:
Https://www.cosc.canterbury.ac.nz/greg.ewing/python_gui/
Tutorial links:
Https://realpython.com/pysimplegui-python/
07. Pyforms
Pyforms is a cross-platform framework for developing GUI applications.
Pyforms is a Python2.7/3.x cross-environment graphics application development framework, modularization and code reuse can save a lot of work.
Allows applications to run on desktops, Web and terminals without changing the code.
# install PyFromspip install-I https://pypi.tuna.tsinghua.edu.cn/simple PyFroms
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.