In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to solve the problem of using pyqt pop-up message prompt boxes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Install pyqt
PyQt5 is a python module that allows you to build GUI applications very quickly. It is usually compared to Tkinter, but there are some differences. One of the main items is the speed of developing GUI. PyQt comes with an amazing tool called Qt-Designer, a drag-and-drop interface that automatically generates code for you based on the GUI you build. This allows you to build beautiful applications more quickly. Another difference is that Tkinter comes with Python 3 packages by default, where PyQt must be installed.
The first step in getting started with PyQt5 is to install it. To do this, we will need to use pip.
Execute the following command to install PyQt5.
$sudo apt-get install python3-dev$ sudo apt-get install python3-pip$ pip3 install pyqt5 $pip3 install pyqt5-tools installation encountered a problem
No matching distribution found for PyQt5-Qt5 > = 5.15.2 (from pyqt5)
Could not find a version that satisfies the requirement PyQt5-Qt5 > = 5.15.2 (from pyqt5) (from versions:)
No matching distribution found for PyQt5-Qt5 > = 5.15.2 (from pyqt5)
This is due to the fact that the pip tool is not updated, execute the following command:
$pip3 install-- upgrade pip3 $pip3 install-- upgrade pyqt5 $pip3 install-- upgrade pyqt5-tools and To avoid this problem you can invoke Python with'- m pip' instead of running pip directly. problem
This is due to the existence of pip for python2. You can use pip3 to uninstall pip, as follows:
$python-m pip uninstall pip
Ubuntu uses Python2 by default to change the default configuration to Python3.
1. View the python version that has been installed on the current ubuntu system and the python version currently in use
$python-version
two。 Set update-alternativer to switch python version
$update-alternatives-list python
Displays after execution:
Update-alternatives: error: no alternatives for python # indicates that the replacement version of Python has not been installed
Then execute the following command to install:
Update-alternatives-- install / usr/bin/python python / usr/bin/python2.7 1update-alternatives: using / usr/bin/python2.7 to provide / usr/bin/python (python) in auto mode$ update-alternatives-- install / usr/bin/python python / usr/bin/python3.6 2update-alternatives: using / usr/bin/python3.6 to provide / usr/bin/python (python) in auto mode
View the default version
$python-version
In the above command, because we set the priority of the / usr/bin/python3.6 setting to 2, the update-alternatives command itself set 3.6 as the default.
Display Python instead of version information again
$update-alternatives-list python
Switch between versions of update-alternatives-- config python
$sudo update-alternatives-config python
Install pip for 3.python3 version
$sudo apt-get install python3-pip
Run the pip3-V display version after installation
Upgrade the pip version
$pip3 install-- upgrade pip test verification
You only need to do the following to verify that pyqt5 is installed and configured properly:
$python3 > from PyQt5 import QtWidgets > quit ()
Create a basic GUI application
After the pyqt5 module has been successfully installed, create a simple GUI to test whether it works.
Refer to the example of Linux Commune. The specific code is as follows:
From PyQt5 import QtWidgetsfrom PyQt5.QtWidgets import QApplication, QMainWindow, QLabelimport sysdef main (): app = QApplication (sys.argv) win = QMainWindow () win.setGeometry (300300600400) win.setWindowTitle ("Linux Commune www.linuxidc.com") label = QLabel (win) label.setText ("Linux Commune") label.move (280,150) label1 = QLabel (win) label1.setText ("www.linuxidc.com") label1.adjustSize () Label1.move (260180) win.show () sys.exit (app.exec_ ()) main () # make sure the function is called
Be sure to keep the indentation of the main Python code consistent, or you will report an error.
The implementation results are as follows:
Sample message dialogs of various styles
Now show the various message dialogs in pyqt5 with a small example, the specific code is as follows:
Import sysfrom PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class MyWindow (QWidget): def _ _ init__ (self,parent=None): super (MyWindow) Self). _ init__ (parent) self.setWindowTitle ("pop-up dialog example") self.resize (400200) self.btn1=QPushButton (self) self.btn1.setText ("message box") self.btn1.clicked.connect (self.msg1) layout=QVBoxLayout () self.btn2=QPushButton (self) self.btn2.setText ("question and answer dialog") Self.btn2.clicked.connect (self.msg2) self.btn3=QPushButton () self.btn3.setText ("warning dialog") self.btn3.clicked.connect (self.msg3) self.btn4=QPushButton () self.btn4.setText ("critical error dialog") self.btn4.clicked.connect (self.msg4) self.btn5=QPushButton () self.btn5.setText ( About dialog boxes) self.btn5.clicked.connect (self.msg5) layout.addWidget (self.btn1) layout.addWidget (self.btn2) layout.addWidget (self.btn3) layout.addWidget (self.btn4) layout.addWidget (self.btn5) self.setLayout (layout) def msg1 (self): # use infomation information box QMessageBox.information (self "title", "message body", QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes) def msg2 (self): QMessageBox.question (self, "title", "question and answer message body", QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes) def msg3 (self): QMessageBox.warning (self, "title", "warning message body", QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes) def msg4 (self): QMessageBox.critical (self, "title" "critical error message body", QMessageBox.Yes | QMessageBox.No,QMessageBox.Yes) def msg5 (self): QMessageBox.about (self, "title", "about message body") if _ _ name__== "_ _ main__": app=QApplication (sys.argv) win=MyWindow () win.show () sys.exit (app.exec_ ())
The effect is as follows:
Message dialog box:
Question and answer dialog box:
Warning dialog box
Critical error dialog box
About dialog boxes:
Python program packaging
We have written and tested the interface program through our own development environment, and if we only provide each other with the Python script we wrote to execute it, the other side's PC environment may not have our program dependent environment. At this time, we'd better package the Python program and send it to each other.
Install pyinstaller
Pip install pyinstaller
Prepare the python file, where you can use the code demo.py that we just used for testing
Package it into an executable file
Terminal input:
Pyinstaller-F demo.py
You can generate the executable file demo:
Enter. / demo in the terminal to run:
Common parameters:
-I add an icon, followed by the icon path. The icon format must be .ico.
-F is packaged into an exe file
-w use window, no console
-c use the console, no window
-D create a directory containing exe and other dependent files
Pyinstaller-h to view the parameters
Install pyinstaller to display command not found
Download it from the pyinstaller official website, and decompress the https://github.com/pyinstaller/pyinstaller after downloading it locally:
$tar-xjf xx.tar.bz2
After the decompression is completed, go to the folder to execute:
$python setup.py install
After the installation is complete, you can go to your file directory and pack it.
Thank you for reading! This is the end of this article on "how to solve the problem of using pyqt pop-up message prompt box". 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 out 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: 288
*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.