当前位置:网站首页>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
边栏推荐
- R语言检验时间序列的平稳性:使用tseries包的adf.test函数实现增强的Dickey-Fuller(ADF)检验、检验时序数据是否具有均值回归特性(平稳性)、不具有均值回归特性的案例
- Interviewer: How to deal with Redis big key?
- shell数组
- 【GORM】模型关系-HasMany关系
- (转)字符集编码标识符,数字表示字符编码
- JuiceFS 在多云存储架构中的应用 | 深势科技分享
- Evolution of MLOps
- 关于ETL的两种架构(ETL架构和ELT架构)
- 2022年中国第三方证券APP创新专题分析
- OFDM 十六讲 7 - Inter-Symbol-Interference
猜你喜欢
js十五道面试题(含答案)
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
MySQL——JDBC
Presto Event Listener开发
【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
leetcode brush questions diary Calculate the number of elements on the right that is less than the current element
Tencent continues to wield the "big knife" to reduce costs and increase efficiency, and free catering benefits for outsourced employees have been cut
(转)FreeType字体位图属性
小程序+自定义插件的关键性
异常处理(try,catch,finally)
随机推荐
异常处理(try,catch,finally)
深度剖析 Apache EventMesh 云原生分布式事件驱动架构
【Apifox】为什么如此受青睐,此篇文章和大家分享
Postgresql源码(68)virtualxid锁的原理和应用场景
华为鸿蒙3.0的野望:技术、应用、生态
Basic operations of openGauss database (super detailed)
C. Omkar and Baseball
Cesium渐变色3dtiles白模(视频)
R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用scale_x_continuous函数的breaks参数指定X轴的断点的个数(设置参数n)
shell数组
杂谈——程序员的悲哀
R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
注意力引导网络用于视网膜图像分割
R语言将列表数据转化为向量数据(使用unlist函数将列表数据转化为向量数据)
js数组对象去重
面试官:Redis 大 key 要如何处理?
月薪5K的运维小白如何成为月薪5W的高级架构师?
电脑系统重装后怎么用打印机扫描出文件?
跨端技术方案选什么好?
EasyExcel使用