PyQt5 is a combination of Digia’s Qt5 application framework and python. It supports both 2.x and 3.x. Official website: www.riverbankcomputing.co.uk/news.
PyQt5 is composed of a series of Python modules. More than 620 classes, 6000 functions and methods. It can run on mainstream operating systems such as Unix, Windows and Mac OS. PyQt5 has two kinds of certificates, GPL and commercial certificate.
PyQt5 class is divided into many modules, the main modules are:
Start of text
Okay, let's open a new chapter, because I am a little familiar with Gui design than others, so I will open a new hole in Gui design first. Explain here. We are using PyQt5, not Tkinter that comes with Python. I'm not that familiar with that, I can say that I don't know it. In this column, we mainly talk about the basics of PyQt5. As for more, I think everyone explores it on their own. After all, it is difficult, and I am also tired. Well, after the introduction of the column, now let's talk about the formal.
First of all, because PyQt5 is a third-party library, if you import it directly, an error will be reported. It must be downloaded. As for how to download third-party libraries for Python, I have already mentioned the Python library in the basic column. Please read it yourself. (Let’s talk about a simpler one...)
In the command prompt/cmd (Windows system, Linux and Apple system sorry I have not used it, please understand.)
pip install PyQt5
It's very simple. If you report an error, you will see a lot of red letters. If the end is...time out, then it is interrupted. Try a few more times. Other search by yourself, after all, there are too many. I think it is better to download the .whl file.
With so much nonsense, let’s take a look at a practical tool that I’ve been boring to make recently. The interface is very simple. After all, I probably only made a few hours.
import sys,sip
from PyQt5.QtWidgets import QApplication, QWidget,QLabel,QPushButton,QCheckBox, QComboBox,QLineEdit
from PyQt5.QtGui import QFont
from PyQt5.QtCore import Qt
classExchange_of_weather_degree_units(QWidget):
def __init__(self):super().__init__()
self.setting()
def setting(self):
self.unit = None
self.choice =QComboBox(self)
self.choice.addItem('℃')
self.choice.addItem('℉')
self.choice.activated[str].connect(self.choice_)
self.choice.move(50,15)
self.number =QLineEdit(self)
self.number.setPlaceholderText('Enter the conversion value')
self.number.move(15,50)
self.arrowhead =QLabel(self)
self.arrowhead.setText('—————— ')
self.arrowhead.setFont(QFont('microsoft Yahei',20))
self.arrowhead.move(165,20)
self.result =QLabel(self)
self.result.setText(' ')
self.result.setFont(QFont('microsoft Yahei',15))
self.result.move(370,27.5)
self.yes =QPushButton('determine',self)
self.yes.clicked.connect(self.yes_)
self.yes.move(220,50)
self.setGeometry(300,100,520,100)
self.setWindowTitle('Conversion of Celsius to Fahrenheit')
self.show()
def choice_(self,text):
self.unit = text
def yes_(self):try:if self.unit =='℃':
result_ =eval(self.number.text())*1.8+32
self.result.setText(str(result_)+'℉')if self.unit =='℉':
result_ =round((eval(self.number.text())-32)/1.8,6)
self.result.setText(str(result_)+'℃')else:
result_ =eval(self.number.text())*1.8+32
self.result.setText(str(result_)+'℃')
except:
self.result.setText('Please enter the number')if __name__ =='__main__':
app =QApplication(sys.argv)
Ex =Exchange_of_weather_degree_units()
sys.exit(app.exec_())
This is a small tool for converting between Fahrenheit and Celsius, which is very practical for me. You don’t need to understand, just copy the past and see the effect.
In fact, there is not much knowledge involved in this, so don't be afraid. Because the most basic PyQt5 framework is like this:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
classExample(QWidget):
def __init__(self):super().__init__()
self.settings()
def settings(self):
self.setGeometry(300,300,450,350)
self.show()if __name__ =='__main__':
app =QApplication(sys.argv)
ex =Example()
sys.exit(app.exec_())
Of course, this is just the simplest model I think, but it can actually be more streamlined than this. But the length is about the same. So don’t be afraid.
As for what Qt5 is, you can search for it yourself. Anyway, I can tell you that this is an extremely powerful and mature library, and Qt itself is actually a tool. If you want, you can add me QQ.
Having said these, we will now analyze the framework code:
The sys library is a standard library:
The role of the sys library: view python interpreter information and pass information to the python interpreter.
sys.argv: Get a list of command line parameters, the first element is the program itself
sys.exit(n): Exit the Python program, exit(0) means normal exit. When the parameter is not 0, a SystemExit exception will be raised, which can be caught in the program
sys.version: Get the version information of the Python interpreter
sys.maxsize: The largest Int value, 64-bit platform is 2**63 – 1
sys.path: returns the search path of the module, using the value of the PYTHONPATH environment variable during initialization
sys.platform: Returns the name of the operating system platform
sys.stdin: input related
sys.stdout: output related
sys.stderr: Error related
sys.exc_info(): returns a triplet of exception information
sys.getdefaultencoding(): Get the current encoding of the system, the default is utf-8
sys.setdefaultencoding(): Set the default encoding of the system
sys.getfilesystemencoding(): Get the file system encoding, the default is utf-8
sys.modules #Return all imported modules in the current Python environment in the form of a dictionary
sys.builtin_module_names: returns a list containing the names of all modules that have been compiled into the Python interpreter
sys.copyright: current copyright information of Python
sys.flags: List of command line identification status information. Read only.
sys.getrefcount(object): returns the number of references to the object
sys.getrecursionlimit(): returns the maximum recursion depth of Python, the default is 1000
sys.getsizeof(object[, default]): returns the size of the object
sys.getswitchinterval(): returns the thread switching interval, the default is 0.005 seconds
sys.setswitchinterval(interval): Set the time interval for thread switching, in seconds
sys.getwindowsversion(): Returns the version information of the current windows system
sys.hash_info: Returns the parameters of the Python default hash method
sys.implementation: The specific implementation of the currently running Python interpreter, such as CPython
sys.thread_info: current thread information
Some of these are for my reference, but mainly to check the code of the PyQt5 library itself, and then translate and explain the results in English.
Then it is unpacking, this is fine.
Then create the Example library, inheriting QWidget.
PyQt5 has many modules, among which QWidget is a module, which contains a series of UI elements for creating desktop applications.
The following initialization code should be no problem.
Then there is this self.setGeometry(300, 300, 450, 350), this is to set the distance and width of the window from the upper left corner of the screen.
Everyone will understand this by changing the data.
Then there is self.show()
, this is to show the interface.
The last is to run, the judgment of if is simply this:
When other documents call your library, the code in if will not be executed.
Then everyone in the app line means that all PyQt5 applications must create an Application object. The sys.argv parameter is a list of parameters from the command line. Python scripts can be run in the shell. This is a method we use to control the startup of our application.
ex is to call the library, then first execute the code of __init__
, and then init calls the settings, so the program in initUI
is executed directly. Finally exit.
Let's change it to look better.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon
classExample(QWidget):
def __init__(self):super().__init__()
self.settings()
def setting(self):
self.setWindowTitle(sys.argv[0])
# self.setWindowIcon(QIcon('Picture name.ico'))Add pictures by yourself
self.setGeometry(300,300,450,350)
self.show()if __name__ =='__main__':
app =QApplication(sys.argv)
ex =Example()
sys.exit(app.exec_())
We added two lines, and the first line in settings
is the setting title. In the second line, you add the ico picture by yourself, which will be richer. I won’t list this method, just remember it yourself.
to sum up
This is the end of this article on the introduction of Python PyQt5. For more related Python PyQt5 introductions, please search for previous articles on ZaLou.Cn or continue to browse related articles below. Hope you will support ZaLou.Cn more in the future!
Recommended Posts