PyQt5型QWidget.setGeometry()不为QLab工作

2024-05-14 09:09:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在PyQt5中开发一个非常简单的应用程序来显示和排序图像。我是Python和Qt的新手,我遇到了一些问题。我尝试在QLabel对象中显示QPixmap图像,并且它们能够正确显示。但是,它们作为默认值排成一行,我不知道如何移动它们。在

(我知道代码很糟糕,但我正在努力让它工作起来,然后再把它弄干净。)

这是我的代码:

class ImageClassifier(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):

        hbox = QtWidgets.QHBoxLayout(self)

        mainImg = QPixmap("img.png")
        xmplImg0 = QPixmap("img0.png")
        xmplImg1 = QPixmap("img1.png")
        xmplImg2 = QPixmap("img2.png")
        xmplImg3 = QPixmap("img3.png")

        lbl = QtWidgets.QLabel(self)
        lbl0 = QtWidgets.QLabel(self)
        lbl1 = QtWidgets.QLabel(self)
        lbl2 = QtWidgets.QLabel(self)
        lbl3 = QtWidgets.QLabel(self)

        lbl.setPixmap(mainImg)
        lbl0.setPixmap(xmplImg0)
        lbl1.setPixmap(xmplImg1)
        lbl2.setPixmap(xmplImg2)
        lbl3.setPixmap(xmplImg3)

        hbox.addWidget(lbl)
        hbox.addWidget(lbl0)
        hbox.addWidget(lbl1)
        hbox.addWidget(lbl2)
        hbox.addWidget(lbl3)

        lbl0.setGeometry(30, 30, 30, 30)

        self.setLayout(hbox)
        self.move(300, 200)
        self.setWindowTitle('Fruit Classifier')
        self.show()

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    ic = ImageClassifier()
    sys.exit(app.exec_())

lbl0.setGeometry()函数根本不起作用,我不知道为什么。有没有人知道解决这个问题的方法,或者设置QLabel对象位置的更好方法?在


Tags: 图像selfpnginitlblqtwidgetsaddwidgetqlabel
1条回答
网友
1楼 · 发布于 2024-05-14 09:09:01

遵循以下项目符号。在

  • 当您向QHBoxLayout/QVBoxLayout添加内容时。。。您还必须设置BoxLayout的几何结构,而不仅仅是其中一个元素的几何结构。在
  • 在BoxLayout中抛出的所有内容都将调整为它的父级(BoxLayout)。在
  • 尝试设置所有元素的几何图形。在
  • 尝试设置BoxLayout的几何图形
  • 如果仍然不起作用,请根据需要为所有元素和BoxLayout设置“.setFixedSize(_width,_height)”。在

[更新] 看看我想说什么。举个例子更好:

如果使用BoxLayout,对象的位置将不符合您的要求。它将遵循BoxLayout策略。您还为QLabels传递了(self)使QWidget成为其父级。真是一团糟。在

检查下面的代码。 我没有BoxLayout,我只是说谁是我(Qlabel)的父母。所以我会像我爸爸说的那样。现在你可以移动它了。在

import sys
from PyQt5 import QtWidgets


class ImageClassifier(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):

        self.setFixedSize(500,500)

        lbl = QtWidgets.QLabel(self)
        lbl.setFixedSize(50,50)
        lbl0 = QtWidgets.QLabel(self)
        lbl0.setFixedSize(50, 50)
        lbl1 = QtWidgets.QLabel(self)
        lbl1.setFixedSize(50, 50)
        lbl2 = QtWidgets.QLabel(self)
        lbl2.setFixedSize(50, 50)
        lbl3 = QtWidgets.QLabel(self)
        lbl3.setFixedSize(50, 50)

        lbl.setStyleSheet("""background-color: red;""")
        lbl0.setStyleSheet("""background-color: green;""")
        lbl1.setStyleSheet("""background-color: blue;""")
        lbl2.setStyleSheet("""background-color: yellow;""")
        lbl3.setStyleSheet("""background-color: black;""")

        lbl0.move(50,50)
        lbl1.move(100, 100)
        lbl2.move(150, 150)
        lbl3.move(150, 150)

        self.move(300, 200)
        self.setWindowTitle('Fruit Classifier')
        self.show()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ic = ImageClassifier()
    sys.exit(app.exec_())

这只是一个简单的方法。最好创建自己的继承QLabel的标签。并根据您的需要进行个性化设置。在

相关问题 更多 >

    热门问题