**
Links to all notes
**
Questions
Fishing – finding things
All modules of PyQt official website (python)
API Documentation for C++ Concrete Implementation
View the properties of the .ui file
You can use the uic module from the PyQt5 module to load a .ui file into a Python application and view the properties it contains.
The following is a simple example code that loads a .ui file named “example.ui” and uses the dir()
function to list all properties of the file:
from PyQt5 import uic # Load the .ui file ui_file = "example.ui" ui = uic.loadUi(ui_file) # List all attributes of the loaded UI file print(dir(ui))
Warning is deprecated
In some cases, when using floating-point numbers for integer arithmetic, a warning message may appear:
DeprecationWarning: an integer is required (got type float). Implicit
conversion to integers using int is deprecated, and may be removed in
a future version of Python. w. move(x – width / 2, y – height / 2)
Translation: Deprecated Warning: An integer is expected (gets type float). The implicit conversion of int to integer is deprecated and may be removed in a future version of Python.
Original code
w.move(x - width / 2, y - height / 2)
Modified code
w.move(int(x - width / 2), int(y - height / 2))
In the modified code, we use the int() function to convert the floating point number to an integer to satisfy the warning requirement.
0. Install
1. Create a new virtual environment
I created it directly with PyCharm without using code
mkvirtualenv -p python3 py3-qt --no-download
2. Install pyqt5 (command line)
Use alt + f5 in PyCharm to quickly open the terminal corresponding to the virtual environment and install pyqt5
pip install pyqt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
3. Installation success test
Enter the following test code in the virtual environment where PyQt is currently installed:
QT_VERSION_STR global variables are generally capitalized
# If the execution is successful and there is no error message, it means that the environment is built successfully from PyQt5 import QtWidgets # Of course, you can also check the PyQt version from PyQt5.QtCore import * print(QT_VERSION_STR)
1. UI
1. The first PyQt program
import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt") # display window w. show() # The program performs a loop waiting state app.exec()
![Program Explanation](https://img-blog.csdnimg.cn/508b47a809ac4fb4aded2c3a7cf5fee4.png
2. Module introduction
There are a lot of functional modules in PyQt, and there are three most commonly used functional modules in development:
- QtCore: Contains core non-GUI functions. Mainly works with time, files and folders, various data, streams, URLs, mime-like files, processes and threads
- QtGui: Contains window system, event handling, 2D images, basic painting, fonts and text classes
- QtWidgets: Contains a series of UI elements for creating desktop applications
You can refer to all modules on the PyQt official website
You can refer to the API documentation for the specific implementation of C++
What function is used depends on its related api or experience shared by others, this is the fastest way to learn
3. Basic UI
All controls in the window, if they want to be displayed in the window, need to indicate who their father is, and cannot be displayed directly using the show function
1. Button
The control name corresponding to the button is QPushButton , located in PyQt5.QtWidgets
# Recognize father after birth import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt program") # Add controls to the window btn = QPushButton("button") # Set the parent of the button to be the current window, equal to adding it to the window for display btn. setParent(w) # display window w. show() # The program performs a loop waiting state app.exec()
2. Text
The name of the plain text control is QLabel, which is located in PyQt5.QtWidgets
The plain text control is only displayed as a logo, similar to a label prompt (account, password) before the input content
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt") # # Create a Label below, and then call the method to specify the parent class # label = QLabel("Account: ", w) # # Set the parent object # label. setParent(w) # Create a Label (plain text) below, and specify the parent object when creating it label = QLabel("Account: ", w) # Display position and size: x, y, w, h label. setGeometry(20, 20, 30, 30) # display window w. show() # The program performs a loop waiting state app.exec()
3. Input box
The control name of the input box is QLineEdit, located in PyQt5.QtWidgets
import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt") # Plain Text label = QLabel("account", w) label. setGeometry(20, 20, 30, 20) # text box edit = QLineEdit(w) edit.setPlaceholderText("Please enter account number") edit.setGeometry(55, 20, 200, 20) # Add controls to the window btn = QPushButton("register", w) btn. setGeometry(50, 80, 70, 30) # display window w. show() # The program performs a loop waiting state app.exec()
4. Adjust window size
import sys from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt") # window size w.resize(300, 300) # display window w. show() # The program performs a loop waiting state app.exec()
5. The window is displayed in the middle of the screen
import sys from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget if __name__ == '__main__': app = QApplication(sys. argv) w = QWidget() # set window title w.setWindowTitle("The first PyQt") # window size w.resize(300, 300) # Set the window to the upper left corner of the screen # w. move(0, 0) # Adjust the window to display in the center of the screen center_pointer = QDesktopWidget().availableGeometry().center() x = center_pointer.x() y = center_pointer.y() # w. move(x, y) # w. move(x-150, y-150) print(w. frameGeometry()) print(w. frameGeometry(). getRect()) print(type(w. frameGeometry(). getRect())) old_x, old_y, width, height = w.frameGeometry().getRect() w. move(x - width / 2, y - height / 2) # display window w. show() # The program performs a loop waiting state app.exec()
not in the center
in the center
- Set the window icon (too ugly, generally not used, directly hide the title bar)
You can download the icon icon website
i
mport sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QApplication, QWidget if __name__ == '__main__': app = QApplication(sys. argv) # Create a QWidget w = QWidget() # set title w.setWindowTitle("Look at my handsome icon") # set icon w.setWindowIcon(QIcon('panda.png')) # show QWidget w. show() app.exec()
The rest: UI design – PyQt5