PyQt5 - QFrame 大小在 window 中被忽略
PyQt5 - QFrame size is ignored in window
我正在创建一个应用程序,左侧是 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()
我希望左边有一个大的红色形状,但我却得到了这个:
调整 window 的大小(在运行时通过拖动或通过在代码中设置几何图形)确实会调整 QFrame 的大小以整齐地填满屏幕的一半。但我希望它具有预定义的固定大小。
为什么 frame.resize
没有按预期工作?
找到了。使用 frame.setFixedSize()
完全符合我的要求:
class MyFrame(QFrame):
def __init__(self, *args, **kwargs):
self.setFixedSize(300, 300) # < Added this line
框架保持其大小,如果我调整整个大小 window 这是尊重的:
我仍然不确定为什么 resize()
什么都不做。
我正在创建一个应用程序,左侧是 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()
我希望左边有一个大的红色形状,但我却得到了这个:
调整 window 的大小(在运行时通过拖动或通过在代码中设置几何图形)确实会调整 QFrame 的大小以整齐地填满屏幕的一半。但我希望它具有预定义的固定大小。
为什么 frame.resize
没有按预期工作?
找到了。使用 frame.setFixedSize()
完全符合我的要求:
class MyFrame(QFrame):
def __init__(self, *args, **kwargs):
self.setFixedSize(300, 300) # < Added this line
框架保持其大小,如果我调整整个大小 window 这是尊重的:
我仍然不确定为什么 resize()
什么都不做。