将标签大小设置为与 QPixmap 图像一样大

set label size as big as QPixmap image

我有一个显示图像的标签,但是由于图像尺寸大于标签,图像被截断了,我试过 self.logo_buttons.SetScaledContents(True)但这只会调整我的图像大小以适合标签。

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class GUI(QMainWindow):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.initUI()

    def initUI(self):
        self.logo_button = QtWidgets.QLabel(self)
        self.logo_button.setPixmap(QtGui.QPixmap('image.png'))
        self.logo_button.setScaledContents(True)


def window():
    app = QApplication(sys.argv)
    root = GUI()

    root.show()
    sys.exit(app.exec_())


window()

您需要做的是使用 adjustSize() 调整 QLabel 的大小:

self.logo_button = QtWidgets.QLabel(self)
self.logo_button.setPixmap(QtGui.QPixmap('image.png'))
<b>self.logo_button.adjustSize()</b>