Python-PyQt5-拆分器-根据像素数拆分屏幕

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

所以我试图用PyQt5创建一个GUI,我需要将GUI如下图所示分割,数字是像素

enter image description here

我试图以特定的像素值启动特定的窗口,并保持这种方式,即使我拉伸窗口,我也希望窗口保持在特定位置并保持相同的大小。因此,几乎所有的窗户都不会改变并且不会移动

我正在尝试使用功能QSplitter,但我找不到任何使它从特定像素值开始且恒定的选项

from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame, 
    QSplitter, QStyleFactory, QApplication)
from PyQt5.QtCore import Qt
import sys

class Example(QWidget):

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

        self.initUI()


    def initUI(self):      

        hbox = QHBoxLayout(self)

        topleft = QFrame(self)
        topleft.setFrameShape(QFrame.StyledPanel)

        topright = QFrame(self)
        topright.setFrameShape(QFrame.StyledPanel)

        bottom = QFrame(self)
        bottom.setFrameShape(QFrame.StyledPanel)

        splitter1 = QSplitter(Qt.Horizontal)
        splitter1.addWidget(topleft)
        splitter1.addWidget(topright)

        splitter2 = QSplitter(Qt.Vertical)
        splitter2.addWidget(splitter1)
        splitter2.addWidget(bottom)

        hbox.addWidget(splitter2)
        self.setLayout(hbox)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QSplitter')
        self.show()
python pyqt pyqt5 qsplitter
1个回答
0
投票

IIUC您希望固定QFrame的大小和位置,如果是固定的,则不应该使用QSplitter,而必须使用move来设置位置,并使用setFixedSize来设置大小:

from PyQt5 import QtCore, QtWidgets


class Example(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        f1 = self.create_qframe(QtCore.QPoint(10, 10), QtCore.QSize(640, 480))
        f2 = self.create_qframe(QtCore.QPoint(10, 500), QtCore.QSize(640, 480))
        f3 = self.create_qframe(QtCore.QPoint(660, 10), QtCore.QSize(320, 240))
        f4 = self.create_qframe(QtCore.QPoint(990, 10), QtCore.QSize(320, 240))
        f5 = self.create_qframe(QtCore.QPoint(660, 260), QtCore.QSize(320, 220))
        f6 = self.create_qframe(QtCore.QPoint(990, 260), QtCore.QSize(320, 220))
        f7 = self.create_qframe(QtCore.QPoint(660, 500), QtCore.QSize(320, 240))
        f8 = self.create_qframe(QtCore.QPoint(990, 500), QtCore.QSize(320, 240))
        f9 = self.create_qframe(QtCore.QPoint(660, 750), QtCore.QSize(320, 220))
        f10 = self.create_qframe(QtCore.QPoint(990, 750), QtCore.QSize(320, 220))

        self.setMinimumSize(1320, 990)
        # or self.setFixedSize(1320, 990)

    def create_qframe(self, pos, size):
        frame = QtWidgets.QFrame(self, frameShape=QtWidgets.QFrame.StyledPanel)
        frame.move(pos)
        frame.setFixedSize(size)
        return frame


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    app.setStyle("fusion")
    w = Example()
    w.show()
    sys.exit(app.exec_())
© www.soinside.com 2019 - 2024. All rights reserved.