Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5-在窗口中忽略QFrame大小_Python_Pyqt5_Qframe - Fatal编程技术网

Python PyQt5-在窗口中忽略QFrame大小

Python PyQt5-在窗口中忽略QFrame大小,python,pyqt5,qframe,Python,Pyqt5,Qframe,我正在创建一个应用程序,左侧有一个QFrame,右侧有一个控制面板。但是,我无法使左侧的QFrame大小正确。我创建了以下示例来演示该问题: import sys from PyQt5.QtWidgets import QFrame, QApplication, QWidget, QVBoxLayout, QHBoxLayout, \ QLabel class MainWindow(QWidget): """Main Windows for th

我正在创建一个应用程序,左侧有一个QFrame,右侧有一个控制面板。但是,我无法使左侧的QFrame大小正确。我创建了以下示例来演示该问题:

import sys
from PyQt5.QtWidgets import QFrame, QApplication, QWidget, QVBoxLayout, QHBoxLayout, \
    QLabel


class MainWindow(QWidget):
    """Main Windows for this demo."""

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

        self.frame = MyFrame(self)

        layout_main = QHBoxLayout(self)
        layout_left = QVBoxLayout()
        layout_right = QVBoxLayout()

        layout_main.addLayout(layout_left)
        layout_main.addLayout(layout_right)

        self.frame.resize(600, 600)
        layout_left.addWidget(self.frame)

        self.label = QLabel('I am on the right')
        layout_right.addWidget(self.label)

        # self.setGeometry(300, 100, 900, 900)

        self.show()


class MyFrame(QFrame):
    """Custom frame."""

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        self.setFrameStyle(QFrame.Panel | QFrame.Raised)
        self.setStyleSheet('QFrame { background-color: red; }')


def main():
    """Main function."""

    app = QApplication([])
    window = MainWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我希望左边有一个大的红色形状,但我得到的是:

调整窗口大小(在运行时通过拖动或通过在代码中设置几何体)确实会调整QFrame的大小,以整齐地填充半个屏幕。但我希望它有一个预定义的固定大小


为什么
frame.resize
不能按预期工作?

找到了它。使用
frame.setFixedSize()
完全符合我的要求:

class MyFrame(QFrame):

    def __init__(self, *args, **kwargs):

        self.setFixedSize(300, 300)  # < Added this line
类MyFrame(QFrame):
定义初始化(self,*args,**kwargs):
self.setFixedSize(300300)#<添加了此行
框架保持其大小,如果我调整整个窗口的大小,这是值得尊重的:


我仍然不确定为什么
resize()
什么都不做。

resize
什么都不做,因为在这种情况下,框架的大小是由小部件所属的布局管理器管理的(即,在这种情况下,左布局)。布局管理器将根据可用空间和各种其他因素(如拉伸因素、大小策略以及小部件的最小和/或最大大小)自动调整小部件的大小。