PyQT5之菜单栏和工具栏


"""
菜单栏
"""

from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2


class ButtonPanel(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        select_btn = QtWidgets.QPushButton("图像选择")

        self.path_label = QtWidgets.QLabel()
        self.path_label.setText("当前未显示图像路径")
        self.path_label.setAlignment(QtCore.Qt.AlignCenter)  # label上居中显示
        self.path_label.setMaximumHeight(50)  # label最大高度设置
        self.path_label.setStyleSheet("background-color:pink;color:green")  # 背景颜色设置
        font = QtGui.QFont()
        font.setBold(True)
        font.setPointSizeF(10)
        self.path_label.setFont(font)

        self.image_label = QtWidgets.QLabel()

        # 方法二:使用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)  # 自动保持比例放缩方式
        self.image_label.setPixmap(pix)  # 设置图像显示
        self.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
        self.image_label.setStyleSheet("background-color:blue;color:green")  # 背景颜色设置

        btn_panel = QtWidgets.QGroupBox("图像文件选择")
        hboxlayout = QtWidgets.QHBoxLayout()
        hboxlayout.addWidget(self.path_label)
        hboxlayout.addWidget(select_btn)
        hboxlayout.addStretch(1)
        btn_panel.setLayout(hboxlayout)

        vboxlayout = QtWidgets.QVBoxLayout()
        vboxlayout.addWidget(self.image_label)
        vboxlayout.addWidget(btn_panel)
        vboxlayout.addStretch(1)
        self.setLayout(vboxlayout)

        # 绑定点击
        select_btn.clicked.connect(self.on_select_image)

    def on_select_image(self):
        fileinfo = QtWidgets.QFileDialog.getOpenFileName(self, "打开图像文件", ".", "图像文件(*.jpg *.png)")
        fileName = fileinfo[0]
        if fileName != "":
            self.path_label.setText(fileName)
            pixmap = QtGui.QPixmap(fileName)
            pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
            self.image_label.setPixmap(pix)   # 设置图像显示


def on_select_image():
    fileinfo = QtWidgets.QFileDialog.getOpenFileName(main_win, "打开图像文件", ".", "图像文件(*.jpg *.png)")
    fileName = fileinfo[0]
    if fileName != "":
        myPanel.path_label.setText(fileName)
        pixmap = QtGui.QPixmap(fileName)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        myPanel.image_label.setPixmap(pix)   # 设置图像显示


def on_tool_image():
    fileinfo = QtWidgets.QFileDialog.getOpenFileName(main_win, "打开图像文件", ".", "图像文件(*.jpg *.png)")
    fileName = fileinfo[0]
    if fileName != "":
        myPanel.path_label.setText(fileName)
        pixmap = QtGui.QPixmap(fileName)
        pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
        myPanel.image_label.setPixmap(pix)   # 设置图像显示


def on_print():
    # fileinfo = QtWidgets.QFileDialog.getOpenFileName(main_win, "打开图像文件", ".", "图像文件(*.jpg *.png)")
    # fileName = fileinfo[0]
    # if fileName != "":
    text = "你点我干嘛?"
    # myPanel.path_label.setText(text)
    # myPanel.path_label.setText(fileName)
    # pixmap = QtGui.QPixmap(fileName)
    # pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio)  # 自动保持比例放缩方式
    # myPanel.image_label.setPixmap(pix)   # 设置图像显示
    myPanel.image_label.setText(text)      # 设置图像显示
    myPanel.image_label.setAlignment(QtCore.Qt.AlignCenter)  # label上的内容居中显示
    myPanel.image_label.setStyleSheet("background-color:white;color:blue")  # 背景颜色设置
    myPanel.image_label.setMaximumHeight(500)  # label最大高度设置
    myPanel.image_label.setMaximumWidth(500)  # label最大高度设置
    box_action.setChecked(False)


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    main_win = QtWidgets.QMainWindow()
    main_win.setWindowTitle("图像浏览显示")

    # 添加菜单栏
    menu = main_win.menuBar()
    file_menu = menu.addMenu("&文件")
    image_action = QtWidgets.QAction("图像选择", main_win)
    image_action.setStatusTip("This is your image...")
    image_action.triggered.connect(on_select_image)
    file_menu.addAction(image_action)

    # 添加工具栏
    toolbar = QtWidgets.QToolBar("MyToolBar")
    main_win.addToolBar(toolbar)
    box_action = QtWidgets.QAction(QtGui.QIcon("./image/icon/特性.png"), "")
    box_action.setCheckable(True)
    box_action.triggered.connect(on_print)

    tool_action = QtWidgets.QAction("圆形", main_win)
    tool_action.setStatusTip("This is your button")
    tool_action.triggered.connect(on_tool_image)
    tool_action.setCheckable(True)
    toolbar.addAction(tool_action)
    toolbar.addAction(box_action)

    myPanel = ButtonPanel()
    main_win.setCentralWidget(myPanel)
    main_win.setMinimumSize(1080, 720)
    main_win.show()
    app.exec_()


posted @ 2024-06-07 15:04  星空28  阅读(5)  评论(0编辑  收藏  举报