当前位置:网站首页>PyQt5: Getting Started Tutorial

PyQt5: Getting Started Tutorial

2022-08-10 00:12:00 pomelo33

Required Environment

  • Windows/linux
  • python3
  • PyQt

Install PyQt5

The following directly uses pip to install PyQt5, here may be pip/pip3, or both, and will not be repeated later

Direct pip install PyQt5

pip install PyQt5

Since Qt Designer has moved from PyQt5 to tools in Python 3.5, we also need to install pyqt5-tools

pip install pyqt5-tools

At this point, the installation of PyQt5 is complete. You can check whether the installation is successful through the following optional operations:

Uploading...Reupload canceled

  • Win+S to call out the main panel, enter designer, if you see a result similar to the one below, it means that PyQt Designer has been installed
  • Enter pyuic5 in cmd. If "Error: one input ui-file must be specified" is returned, the installation is successful.

Using PyQt5

New Project

Win+S to call out the main panel, enter designer, and click Open.

Uploading...Reupload canceled

The above window will pop up, you can open the previously edited .ui file, or you can click to create a Main Window.After creation, you can see the following screen

Uploading...Reupload canceled

  • The "Widget Box" on the left is a variety of components that can be freely dragged
  • The "MainWindow - untitled" form in the middle is the canvas
  • The "Object Viewer" at the top right can view the structure of the current ui
  • The "Properties Editor" in the middle of the right can set the properties of the currently selected component
  • The "Resource Browser" at the bottom right can add various materials, such as pictures, backgrounds, etc., currently you can ignore it

Add Components

You can drag the tool on the left to the current canvas. For example, I have selected several Push Buttons and a textBrowser here.

After designing the basic layout, click File->Save As in the upper left corner to generate a .ui file.Suggestions can be saved to the folder where the code needs to be called later.

Generate .py file

cd to the folder where the .ui file is located, open cmd, and enter the following command to generate the .py file shown below.

pyuic5 -o name.py name.ui

Run .py file

The generated py file cannot be run directly. We can write the following code in our main file. When running the main file, our designed UI will be displayed.

import sysfrom PyQt5.QtWidgets import QApplication, QMainWindowimport gui_file_nameif __name__ == '__main__':app = QApplication(sys.argv)MainWindow = QMainWindow()ui = gui_file_name.Ui_MainWindow()ui.setupUi(MainWindow)MainWindow.show()sys.exit(app.exec_()

Configuration component interface

The ui is displayed, but the buttons above are still unresponsive when pressed.So now you need to configure it and set the action after the button is triggered.

We need to import partial in the header file first

from functools import partial

Partial is used as follows:

partial(function, arg1, arg2, ...)

Add the following code after MainWindow.show():

ui.pushButton.clicked.connect(partial(process, arg1, arg2))

where process is the function you want the button to call, and arg1 and arg2 are the formal parameters passed in by the function.

Run main.py again, click the button, the corresponding function will be executed.At this time, if you want to add the output printed to the console to the textBrowser, you can add the following function:

def print_qt(mes):ui.textBrowser.append(mes) # Display prompt information in the specified areaui.cursot = ui.textBrowser.textCursor()ui.textBrowser.moveCursor(ui.cursot.End)QApplication.processEvents()

Replace the print in the function.Effect display:

At this point, the general operation of PyQt5 has been displayed.Of course, it also contains many components, and the specific usage can be found in the official documentation: QtGui Module | Qt 4.8

原网站

版权声明
本文为[pomelo33]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092059042379.html