from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2
def click_btn1():
print("click button 1")
txt_label.setText("我是按钮1点击之后")
def click_btn2():
print("click button 2")
def click_btn3():
print("click button 3")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_win = QtWidgets.QMainWindow()
main_win.setWindowTitle("按钮组件 演示")
btn1 = QtWidgets.QPushButton("按钮1")
btn2 = QtWidgets.QPushButton("按钮2")
btn3 = QtWidgets.QPushButton("按钮3")
btn1.clicked.connect(click_btn1)
btn2.clicked.connect(click_btn2)
btn3.clicked.connect(click_btn3)
image_label = QtWidgets.QLabel()
txt_label = QtWidgets.QLabel()
# 方法一:使用QtGui直接显示
# pixmap = QtGui.QPixmap("./image/img1.png")
# pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
# 方法二:使用cv2显示
src = cv2.imread("./image/img1.png") # BGR
image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) # 将BGR转为RGB
h, w, c = image.shape
img = QtGui.QImage(image.data, w, h, 3*w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
image_label.setPixmap(pix) # 设置图像显示
image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
# main_win.setCentralWidget(image_label)
txt_label.setText("Hello, PyQT5")
txt_label.setAlignment(QtCore.Qt.AlignCenter) # label上居中显示
txt_label.setStyleSheet("background-color:pink;color:green") # 背景颜色设置
font = QtGui.QFont()
font.setBold(True)
font.setPointSizeF(32)
txt_label.setFont(font)
label3 = QtWidgets.QLabel()
label3.setText("你好, OpenCV")
label3.setStyleSheet("background-color:gray;color:red") # 背景颜色设置
panel2 = QtWidgets.QWidget()
hvoxlayout = QtWidgets.QVBoxLayout()
hvoxlayout.addWidget(txt_label)
hvoxlayout.addWidget(label3)
panel2.setLayout(hvoxlayout)
btn_panel = QtWidgets.QWidget()
btn_layout = QtWidgets.QHBoxLayout()
btn_layout.addWidget(btn1)
btn_layout.addWidget(btn2)
btn_layout.addWidget(btn3)
btn_panel.setLayout(btn_layout)
panel1 = QtWidgets.QWidget()
hhoxlayout = QtWidgets.QHBoxLayout()
hhoxlayout.addWidget(panel2)
hhoxlayout.addWidget(image_label)
hhoxlayout.addWidget(btn_panel)
panel1.setLayout(hhoxlayout)
main_win.setCentralWidget(panel1)
main_win.setMinimumSize(1080, 720)
main_win.show()
app.exec_()