【问题标题】:Python PyQt5: Image doesn't loadPython PyQt5:图像未加载
【发布时间】:2016-10-31 01:00:11
【问题描述】:

我想加载图像如果我点击一个按钮但只显示图像的一个小像素。

看起来是这样的:

class MyWindow(QWidget):
    def __init__(self):
        super().__init__() 
        self.resize(1000, 1000)
        self.setWindowTitle("MyWindow")
        self.setWindowIcon(QIcon("myIcon.ico"))
        self.setMaximumSize(width, height)
        self.setMinimumSize(1000, 1000)

        self.canvas = QGroupBox(self)
        self.canvas.setStyleSheet("QGroupBox { border: 1px solid #9F9B9B}")
        self.canvas.move(350, 30)
        self.canvas.resize(210, 220)

        self.bImage = QPushButton("Load Image", self)
        self.bImage.move(150, 207)
        self.bImage.clicked.connect(self.openImage)

        self.show()      

    def openImage(self):                    
        self.label = QLabel(self)
        self.preview = QPixmap("image.png")
        self.label.setPixmap(self.preview)
        self.label.move(350, 30)     

但奇怪的是,如果我将 openImage() 函数中的代码放入 init() 函数的第一行,图像将完全显示。

如何通过 openImage() 函数加载整个图像?

【问题讨论】:

    标签: python image pyqt5 pixmap


    【解决方案1】:

    尝试使用绝对值定位小部件通常是个坏主意。您应该始终尽可能使用布局。图像不显示的原因是您将标签移动到组框后面的位置。相反,您应该将标签放在组框的布局中:

    class MyWindow(QtGui.QWidget):
        def __init__(self):
            ...
            self.canvas = QtGui.QGroupBox(self)
            ...
            self.label = QtGui.QLabel(self)
    
            layout = QtGui.QVBoxLayout(self.canvas)
            layout.addWidget(self.label)
            ...    
    
        def openImage(self):
            self.preview = QtGui.QPixmap("image.png")
            self.label.setPixmap(self.preview)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 1970-01-01
      相关资源
      最近更新 更多