Categories
python

QT 使用图片作为背景

convertToQtFormat = QtGui.QImage(rgbImage.data, rgbImage.shape[1], rgbImage.shape[0], QImage.Format_RGB888)
									#在这里可以对每帧图像进行处理,
									image = convertToQtFormat.scaled(440, 330, Qt.KeepAspectRatio)
									self.video.setPixmap(QPixmap.fromImage(image))

rgbImage.data 是 python图片数组数据

所有组件都可以通过 setPixmap(QPixmap.fromImage(image)) 设置背景图像

import sysfrom PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_MainWindow(object):

def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(270, 180, 241, 101))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(":/img/logo.png"))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

if __name__ == '__main__':import resource  
# 导入添加的资源(根据实际情况填写文件名)
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

设置icon

        self.pushButton_9.setIcon(QtGui.QIcon(QtGui.QPixmap("./resources/setting.png")))
self.pushButton_9.setIconSize(QtCore.QSize(30,30))
Categories
python

QT designer 使用

使用QT designer会生成一系列ui后缀文件

通过pyuic可以将ui文件转化为py文件

python -m PyQt5.uic.pyuic 1.ui -o 1.py
pyuic5 1.ui -o 1.py
pyside6-uic form.ui -o ui_form.py

把1.ui 转换为 1.py

class MainForm(QtWidgets.QMainWindow):
	def __init__(self,parent=None):
		super(MainForm,self).__init__(parent)
		__content = Ui_MainWindow.Ui_MainWindow()
		__content.setupUi(self)
		
	

if  __name__ == "__main__" :
	try: 
		print('start')
		__app = QtWidgets.QApplication(sys.argv) 
		__win = MainForm()
		__win .show()
		sys.exit(__app.exec_())
	except KeyboardInterrupt :
		print('destroy')

__content = Ui_MainWindow.Ui_MainWindow()

这段代码可以换成不同的UI generate的代码 就可以 切换ui文件 看效果

Categories
python

Pyqt 组件说明