In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to quickly build a simple GUI application with PyQt5. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. Introduction
The three frameworks commonly used in Python GUI are: Tkinter, wxpython and PyQt5
PyQt5 is based on Qt and is a combination of Python and Qt. Cross-platform GUI applications can be written in Python language.
Compared with Tkiner and wxpython,PyQt5, a powerful GUI application with beautiful interface can be developed very quickly by using its own Qt Designer visualization tools for interface design.
two。 Installation
Take Mac OSX as an example, the operation under PC is similar.
First, install the qt application inside the system
# install qtbrew install qt
Then, install sip and pyqt5 dependent libraries in the Python virtual environment
# installation depends on sippip3 install sip# installation depends on pyqt5pip3 install pyqt5
Next, configure External Tools in Pycharm and create two new commands, Qt Designer and PyUIC
# Qt Designer configure Program:/usr/local/Cellar/qt/5.14.1/libexec/Designer.appWorking directory:/usr/local/Cellar/qt/5.14.1/libexec# PyUIC# Python environment path Program:/usr/local/bin/python3.7# parameter Arguments:-m PyQt5.uic.pyuic $FileName$-o $FileNameWithoutExtension$.py # path [fixed] Working directory:$ProjectFileDir$
In the first step, under the folder of installing Qt applications, find the Designer.app and libexec files and configure them.
Because the files generated by QtDesigner are * .ui files, we need to convert ui files into Python files with the help of pyuic in pyqt5.
3. Introduction to common functions
QtDesigner is a very powerful GUI design tool, and the generated file format is: * .ui
Step 2, select the control, and right-click to add properties for the control, such as control content and control name (equivalent to id). For some complex property settings, you need to set them separately in the properties area.
After editing the UI interface, a * .ui file is generated locally
Step 3, select the * .ui file and use the PyUIC command in Pycharm to convert the ui file to a py file
# ui file to py file from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow (object): def setupUi (self, MainWindow): MainWindow.setObjectName ("MainWindow") MainWindow.resize (800,790) self.centralwidget = QtWidgets.QWidget (MainWindow) self.centralwidget.setObjectName ("centralwidget") self.pushButton = QtWidgets.QPushButton (self.centralwidget) self.pushButton.setGeometry (QtCore.QRect (130,80,111) 41) self.pushButton.setObjectName ("pushButton") self.username = QtWidgets.QLineEdit (self.centralwidget) self.username.setGeometry (QtCore.QRect (260,80,271,41) self.username.setObjectName ("username") self.pushButton_2 = QtWidgets.QPushButton (self.centralwidget) self.pushButton_2.setGeometry (QtCore.QRect (130,150,111) 41) self.pushButton_2.setObjectName ("pushButton_2") self.password = QtWidgets.QLineEdit (self.centralwidget) self.password.setGeometry (QtCore.QRect (260150271,41) self.password.setObjectName ("password") self.login = QtWidgets.QPushButton (self.centralwidget) self.login.setGeometry (QtCore.QRect (260230141) 61) self.login.setObjectName ("login") MainWindow.setCentralWidget (self.centralwidget) self.statusbar = QtWidgets.QStatusBar (MainWindow) self.statusbar.setObjectName ("statusbar") MainWindow.setStatusBar (self.statusbar) self.retranslateUi (MainWindow) QtCore.QMetaObject.connectSlotsByName (MainWindow) def retranslateUi (self, MainWindow): _ translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle (_ translate ("MainWindow") "MainWindow") self.pushButton.setText (_ translate ("MainWindow", "user name:") self.pushButton_2.setText (_ translate ("MainWindow", "password:") self.login.setText (_ translate ("MainWindow", "login"))
Step 4, add a click event to the button
Use the control name. In the form of signal .connect (slot function) to add a listen for click events to the control
Class Ui_MainWindow (object): def setupUi (self, MainWindow): # specify event # clicked: signal for login button Login_met: slot function self.login.clicked.connect (self.login_met) self.retranslateUi (MainWindow) QtCore.QMetaObject.connectSlotsByName (MainWindow) def login_met (self): "" login event (slot function): return: "# contents of the user name input box username = self.login.text () # contents of the password input box password = self.password.text () print ("the user name entered is:" Username, "password is:", password)
Step 5, show the interface
Finally, create a new py file, instantiate a QApplication object, and display the MainWindow created above.
Import sysfrom PyQt5.QtWidgets import QApplication, QMainWindow# interface file from temp import * class CustomUI (QMainWindow, Ui_MainWindow): def _ _ init__ (self, parent=None): super (CustomUI, self). _ init__ (parent) self.setupUi (self) if _ _ name__ ='_ _ main__': app = QApplication (sys.argv) cutomUI = CustomUI () cutomUI.show () sys.exit (app.exec_ ())
It should be noted that this section only calls the logic that displays the GUI interface, and there are few changes in the actual project development process.
Thank you for reading! This is the end of this article on "how to quickly build a simple GUI application with PyQt5". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.