简体   繁体   English

PyQt5-调整标签大小以填充整个窗口

[英]PyQt5 - resize label to fill the whole window

from PyQt5 import QtGui, QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.label = QtWidgets.QLabel(self)
        self.label.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
        self.label.resize(800, 600)
        self.label.setContentsMargins(0, 0, 0, 0);
        self.pixmap = QtGui.QPixmap("image.jpg")
        self.label.setPixmap(self.pixmap)
        self.label.setMinimumSize(1, 1)
        self.label.installEventFilter(self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.label)

    def eventFilter(self, source, event):
        if (source is self.label and event.type() == QtCore.QEvent.Resize):
            self.label.setPixmap(self.pixmap.scaled(
                self.label.size(), QtCore.Qt.KeepAspectRatio))
        return super(Window, self).eventFilter(source, event)

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    window.resize(800, 600)
    sys.exit(app.exec_())

This is my application, my goal is simple - have an image that fills the whole window and resizes after the window resize. 这是我的应用程序,我的目标很简单-在整个窗口中设置图像,并在窗口调整大小后重新调整大小。

This code works okay in resizing the image, but the label doesn't cover the whole window, I have those "borders". 这段代码可以调整图像大小,但是标签不能覆盖整个窗口,我有那些“边界”。 How can I remove them/resize the label to window size? 如何删除它们/将标签调整为窗口大小? I am working on Windows if this changes things. 如果这会改变事情,我正在Windows上工作。

窗口外观

That is the effect I get now. 那就是我现在得到的效果。

I solved it in PyQt4, so I'm not 100% sure if it will work for PyQt5, but I guess it should (some minor modifications might be needed, eg import PyQt5 instead of PyQt4). 我在PyQt4中解决了该问题,所以我不确定100%是否适用于PyQt5,但我想应该这样做(可能需要进行一些小的修改,例如,导入PyQt5而不是PyQt4)。

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel(self)
        self.label.resize(800, 600)
        pixmap1 = QtGui.QPixmap("image.png")
        self.pixmap = pixmap1.scaled(self.width(), self.height())
        self.label.setPixmap(self.pixmap)
        self.label.setMinimumSize(1, 1)

    def resizeEvent(self, event):
        pixmap1 = QtGui.QPixmap("image.png")
        self.pixmap = pixmap1.scaled(self.width(), self.height())
        self.label.setPixmap(self.pixmap)
        self.label.resize(self.width(), self.height())

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    window.resize(800, 600)
    sys.exit(app.exec_())

The most important for you is definition of resizeEvent . 对您来说最重要的是resizeEvent定义。 You could use already defined self.pixmap and just resize it, but the quality of the image would degrade the more resizing you use. 您可以使用已经定义的self.pixmap并调整其大小,但是图像的质量会随着您使用的调整大小而降低。 Therefore, it's better always create new pixmap scaled to current width and height of the Window . 因此,最好总是创建缩放到当前Window widthheight的新pixmap

No need to create a QLabel inside the separate QWidget . 无需在单独的QWidget创建QLabel You can simply inherit QLabel instead of QWidget . 您可以简单地继承QLabel而不是QWidget It will make your code more simple and cleaner: 它将使您的代码更加简单和简洁:

class MyLabelPixmap(QtWidgets.QLabel):
    def __init__(self):
        QtWidgets.QLabel.__init__(self)
        self.setSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Ignored)
        self.resize(800, 600)
        self.pixmap = QtGui.QPixmap("image.jpg")
        self.setPixmap(self.pixmap)
        self.installEventFilter(self)

    def eventFilter(self, source, event):
        if (source is self and event.type() == QtCore.QEvent.Resize):
            self.setPixmap(self.pixmap.scaled(self.size()))
        return super(Window, self).eventFilter(source, event)

In case you would like to embed your MyLabelPixmap widget into the QMainWindow just add in your QMainWindow.__init__ 如果您想将MyLabelPixmap小部件嵌入到QMainWindow只需添加QMainWindow.__init__

self.myLabelPixmap = MyLabelPixmap()
self.setCentralWidget(self.myLabelPixmap) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM