【发布时间】: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