如何在QLabel中调整图片大小?

问题描述 投票:0回答:1

由于图片很大,因此无法根据QLabel中的设计拉伸来缩放它们。

下面是我的代码:


class goShow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initGUI()
        self.filePath = os.path.dirname(__file__)

    def initGUI(self):
        # self.setAcceptDrops(True)
        # self.resize(800, 600)
        widget = QWidget()
        self.setCentralWidget(widget)


        self.resTable = QTableWidget()
        # self.dotPlot = PictureLabel('****')
        self.dotPlot = QLabel()
        # self.dotPlot.setStyleSheet("background: rgb(255, 0, 0)")
        self.barPlot = QLabel()
        # self.barPlot.setStyleSheet("background: rgb(0, 255, 0)")

        layout = QVBoxLayout()
        widget.setLayout(layout)
        self.mainLayout = layout

        self.mainLayout.addWidget(self.resTable, stretch=4)
        self.mainLayout.addWidget(self.dotPlot,stretch=1)
        self.mainLayout.addWidget(self.barPlot,stretch=1)

        self.show()

    def showTable(self, input):
        #show talbe
        dim = input.shape
        self.resTable.setRowCount(dim[0])
        self.resTable.setColumnCount(dim[1])

        for i in range(int(dim[0])):
            for j in range(int(dim[1])):
                print(i, j)
                self.resTable.setItem(i, j, QTableWidgetItem(str(input.iloc[i, j])))

    def showDotPlot(self):
        dotPng = QPixmap(os.path.join('F:\\job\\projects\\snpExplore\\test\\res_temp',"dotplot.png"))
        self.dotPlot.setPixmap(dotPng)
        self.dotPlot.setScaledContents(True)

    def showBarPlot(self):
        # show barplot
        barPng = QPixmap(os.path.join('F:\\job\\projects\\snpExplore\\test\\res_temp',"barplot.png"))
        self.barPlot.setPixmap(barPng)
        self.barPlot.setScaledContents(True)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = goShow()
    goResTable = pd.read_csv("F:\\job\\projects\\snpExplore\\test\\res_temp\\go.csv", header=0)
    w.showTable(goResTable)
    w.showBarPlot()
    w.showDotPlot()
    sys.exit(app.exec_())

这里是获得的图片:

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9uTTNoSC5wbmcifQ==” alt =“在此处输入图像描述”>“ >>

第二张和第三张图片太大,使得第一张桌子很小。但我希望小部件尺寸的比例分别为4:1:1。

由于图片的尺寸很大,因此无法根据QLabel中的设计拉伸来缩放它们。这是我的下面代码:class goShow(QMainWindow):def __init __(self):super().__ init __()...

python image pyqt5 stretch
1个回答
0
投票

如果没有为标签设置最小尺寸,则将始终使用像素图尺寸。为了避免这种情况,您可以设置一个任意的最小大小:

© www.soinside.com 2019 - 2024. All rights reserved.