当前位置:网站首页>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
边栏推荐
- 【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
- R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用scale_x_continuous函数的breaks参数指定X轴的断点的个数(设置参数n)
- R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
- Space not freed after TRUNCATE table
- Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
- 信息系统项目管理师---第十一章项目风险管理历年考题
- FileZilla搭建FTP服务器图解教程
- Redis
- 深度学习100例 —— 循环神经网络(RNN)实现股票预测
- leetcode:331. 验证二叉树的前序序列化
猜你喜欢
随机推荐
R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用scale_x_continuous函数的breaks参数指定X轴的断点的个数(设置参数n)
Let's talk about what DDL, DML, DQL and DCL are in SQL statements
Presto Event Listener开发
工作经验-组件封装(拖拽排序组件)
PyQt5:入门使用教程
Janus官方DEMO介绍
使用股票量化交易接口需要具备怎么样的心态
华为鸿蒙3.0的野望:技术、应用、生态
OSS文件上传
shell学习
Activiti7审批流
反射机制篇
R语言拟合ARIMA模型并使用拟合模型进行预测推理:使用forecast函数计算ARIMA模型未来值(包含时间点、预测值、两个置信区间)
R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
leetcode:325. 和等于k的最长子数组长度
每日一R「02」所有权与 Move 语义
守护进程
【微服务~Nacos】Nacos服务提供者和服务消费者
面试官:Redis 大 key 要如何处理?
电脑系统重装后怎么用打印机扫描出文件?