这周接到了一个任务,是完成ui界面的设计。选择的是pyqt,也就是用python语言进行软件设计。我以为是和labview一样完全图形编程,心想这不是很好上手吗哈哈,就说两天能搞定,现在是第二天了,才弄懂到底怎么用pyqt编程.....
案例1首先从最简单的例子说明下pyqt
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv) #创建一个应用程序
#控件操作
window = QWidget()
window.setWindowTitle("01")
window.resize(500,500)
window.move(400,200)
label = QLabel(window)
label.setText("hello world")
label.move(200,200)
window.show()
sys.exit(app.exec_()) #开始执行应用程序,并进入消息循环
能得到如图所示的程序框
sys.argv首先说明下 sys.argv 的作用
argv = sys.argv print(argv)
运行以上代码块,能得到模块的全路径
['C:/Users/xiaomi/PycharmProjects/uisheji/01.py']
当别人通过命令启动这个程序的时候,可以设定一种功能,如接受命令行传递的参数,来执行不同的业务逻辑。
sys.exit()退出程序,会返回退出码。正常情况下是0,如果因为bug导致不正常退出,退出码是1。会返回不同的退出码反应是因为什么原因推出。
Process finished with exit code 0第一步,导入需要的包和模块
from PyQt5.Qt import * #主要包含了我们常用的类 import sys第二步,创建一个应用程序对象
括号中加入sys.argv 。通过命令行传递脚本的时候可以传递一些参数给整个应用程序,
app = QApplication(sys.argv)第三步,控件的操作 创建控件
刚创建好控件时,没有什么父控件的情况下,默认情况下不会被展示,只有手动show
window = QWidget()设置控件
顶层控件都能设置标题内容等
控件作为容器承载其他控件,其他控件为子控件,会被自动展示。
label = QLabel(window)
label.setText("hello world")
label.move(200,200)
展示控件
第四步,应用程序的执行,进入到消息循环
让整个程序执行,进入到消息循环,让窗口一直处于保持状态,监测用户的交互信息,并且退出时能够反映退出码。
sys.exit(app.exec_())添加快速模板
在Settings中进入搜索live,
点击+,选择Live Templa
加入代码,设置名称,加入$ $能让光标直接定位,之后能直接调用,如图。
实战——自己写的程序自己写的主要是显示图片,(本来要做处理,队友直接用c++实现了,所以我显示图片主要是从固定路径调用图片),读取显示txt,做一些界面上的优化等,还是挺简单的。
# -*- coding = utf-8 -*-
# @Time : 2022/4/28 1:15
# @Author : yeye
# @File : dutu6.py
# @software: PyCharm
import qdarkstyle
import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDockWidget, QListWidget
from PyQt5.QtGui import *
from xiangmu import Ui_MainWindow # 导入创建的GUI类
class mywindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(mywindow, self).__init__()
self.setupUi(self)
self.button1.clicked.connect(self.pushbutton_fuction1)
self.button2.clicked.connect(self.pushbutton_fuction2)
self.button3.clicked.connect(self.pushbutton_fuction3)
self.button4.clicked.connect(self.pushbutton_fuction4)
self.button5.clicked.connect(self.pushbutton_fuction5)
self.button6.clicked.connect(self.pushbutton_fuction6)
self.button7.clicked.connect(self.pushbutton_fuction7)
def pushbutton_fuction7 (self):
fileAddress = "C:/Users/xiaomi/Desktop/tupian/1.txt" # 文件路径
file = open(fileAddress, 'r') # fileAddress为txt文件的路径
fileContent = file.read() # 读取文件内容
file.close()
self.lineEdit.setText(fileContent) # 文本框变量名为textEdit
def pushbutton_fuction1 (self):
Im1 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/01.jpg')
image_height1, image_width1, image_depth1 = Im1.shape
QIm1 = cv2.cvtColor(Im1, cv2.COLOR_BGR2RGB)
QIm1 = QImage(QIm1.data, image_width1, image_height1,
image_width1 * image_depth1,
QImage.Format_RGB888)
self.pic1.setPixmap(QPixmap.fromImage(QIm1))
def pushbutton_fuction2 (self):
Im2 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/02.jpg')
image_height2, image_width2, image_depth2 = Im2.shape
QIm2 = cv2.cvtColor(Im2, cv2.COLOR_BGR2RGB)
QIm2 = QImage(QIm2.data, image_width2, image_height2,
image_width2 * image_depth2,
QImage.Format_RGB888)
self.pic2.setPixmap(QPixmap.fromImage(QIm2))
def pushbutton_fuction3 (self):
Im3 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/03.jpg')
image_height3, image_width3, image_depth3 = Im3.shape
QIm3 = cv2.cvtColor(Im3, cv2.COLOR_BGR2RGB)
QIm3 = QImage(QIm3.data, image_width3, image_height3,
image_width3 * image_depth3,
QImage.Format_RGB888)
self.pic3.setPixmap(QPixmap.fromImage(QIm3))
def pushbutton_fuction4 (self):
Im4 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/04.jpg')
image_height4, image_width4, image_depth4 = Im4.shape 。
QIm4 = cv2.cvtColor(Im4, cv2.COLOR_BGR2RGB)
QIm4 = QImage(QIm4.data, image_width4, image_height4,
image_width4 * image_depth4,
QImage.Format_RGB888)
self.pic4.setPixmap(QPixmap.fromImage(QIm4))
def pushbutton_fuction5 (self):
Im5 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/05.jpg')
image_height5, image_width5, image_depth5 = Im5.shape 。
QIm5 = cv2.cvtColor(Im5, cv2.COLOR_BGR2RGB)
QIm5 = QImage(QIm5.data, image_width5, image_height5,
image_width5 * image_depth5,
QImage.Format_RGB888)
self.pic5.setPixmap(QPixmap.fromImage(QIm5))
def pushbutton_fuction6 (self):
Im6 = cv2.imread('C:/Users/xiaomi/Desktop/tupian/06.jpg')
image_height6, image_width6, image_depth6 = Im6.shape
QIm6 = cv2.cvtColor(Im6, cv2.COLOR_BGR2RGB)
QIm6 = QImage(QIm6.data, image_width6, image_height6,
image_width6 * image_depth6,
QImage.Format_RGB888)
self.pic6.setPixmap(QPixmap.fromImage(QIm6))
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
app.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
window = mywindow()
window.show()
sys.exit(app.exec_())
最终结果显示如图
后记从接到这个任务到现在过了五天,基本是从0开始学开始做(python基础也不是太牢),第三天就做完了基本功能,后期就是一些优化之类的,b站上的教学视频一言难尽,,,好的视频有,就是时长太长,很难快速入门,有时间短的,,看了几个小时感觉没啥用,就是来卖课的,对白嫖党很不友好,最后就是看着别人的代码学的,也算是在规定时间内完成了任务。
感悟就是不要陷入自我内耗,,找教程真的找了好久,很多教程质量不是太好,学了浪费时间,还不如直接研究别人代码快速入门。



