【问题标题】:Update image on a label PyQT5更新标签 PyQT5 上的图像
【发布时间】:2018-08-05 16:51:58
【问题描述】:

我是 python 新手。我想更新 label1 上的图像。但是函数'def browse'总是给我空白标签。似乎代码没有获取图像或更新标签失败,并且程序没有给出任何错误消息。我在这里错过了什么吗?此外,执行 setPixmap 命令下方的打印命令。我正在使用 python 2.7 和 PyQT4。

提前致谢

from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
import cv2
from PIL.ImageQt import ImageQt


class GuiCakep(QtGui.QWidget):

    global label1, label2

    def __init__(self):
        super(GuiCakep, self).__init__()
        self.initUI()


    def initUI(self):
        self.setGeometry(100, 100, 1000, 600)
        self.setWindowTitle('Single Browse')

        self.label1 = QtGui.QLabel(self)
        self.label1.setFrameShape(QtGui.QFrame.Box)
        self.label1.setGeometry(QtCore.QRect(20, 10, 451, 451))
        pixmap = QPixmap('1stimage.jpg')
        self.label1.setPixmap(pixmap)
        self.label2 =QtGui.QLabel(self)
        self.label2.setFrameShape(QtGui.QFrame.Box)
        self.label2.setGeometry(QtCore.QRect(481, 10, 451, 451))
        btn = QtGui.QPushButton('Browse', self)
        btn.resize(btn.sizeHint())
        btn.clicked.connect(self.browse)
        btn.move(775, 500)       
        self.show()

    def browse(self):
        filePath = QtGui.QFileDialog.getOpenFileName(self, 'a file','*.jpg')
        fileHandle = open(filePath, 'r')
        pixmap = QPixmap('filePath')
        self.label1.setPixmap(pixmap)
        print("Whoa awesome")



def main():
    app = QtGui.QApplication(sys.argv)
    w = GuiCakep()
    app.exec_()


if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python-2.7 image-processing pyqt4 qtgui qtcore


    【解决方案1】:

    您正在创建一个以'filePath' 作为参数的像素图。这是一个字符串,而不是您的变量 filePath
    删除两个',这应该会更新标签。

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 2018-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      相关资源
      最近更新 更多