我试图让两个 QFrames 恰好占据 QMainWindow 的一半。强制偏移,我似乎无法摆脱它
I'm trying to hawe two QFrames take exactly half of the QMainWindow. An offset is being forced and I can't seem to get rid of it
我只是在试验 PyQt5,所以我可以找出我正在编写的另一个程序中的一些问题,但最重要的是我似乎无法弄清楚如何准确地更改小部件的几何形状。
我用两个不同颜色的帧写了这个小测试 window,我试图让它们拆分整个可能的 space。
class Test(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Test")
self.setGeometry(QRect(QPoint(300, 300), QSize(300, 300)))
self._centralWidget = QWidget()
self._centralLayout = QHBoxLayout()
self.setCentralWidget(self._centralWidget)
self._centralWidget.setLayout(self._centralLayout)
self.initGUI()
self.show()
def initGUI(self):
self.leftFrame = QFrame()
self.leftFrame.setStyleSheet("background-color:red")
self.leftFrame.setGeometry(QRect(QPoint(0, 0), QSize(150, 300))) # Same as QRect(0, 0, 150, 300)
self.rightFrame = QFrame()
self.rightFrame.setStyleSheet("background-color:green")
self.rightFrame.setGeometry(QRect(QPoint(150, 0), QSize(150, 300))) # Same as QRect(150, 0, 150, 300)
self._centralLayout.addWidget(self.leftFrame)
self._centralLayout.addWidget(self.rightFrame)
MainWindow是300x300,所以我把两个frame设置为150x300。但是,当我 运行 它时,有一个令人讨厌的 9x9 偏移量。所以两个框架的真实大小是: left frame: QRect(9, 9, 138, 282)
|右框:QRect(153, 9, 138, 282)
.
我怀疑可能是 QHBoxLayout 弄乱了大小,但我不确定。任何建议表示赞赏!
如果您想手动设置几何图形,那么您不应该使用布局,因为布局的 objective 是根据其特性自动处理它们。
def initGUI(self):
self.leftFrame = QFrame(self._centralWidget)
self.leftFrame.setStyleSheet("background-color:red")
self.leftFrame.setGeometry(QRect(QPoint(0, 0), QSize(150, 300)))
self.rightFrame = QFrame(self._centralWidget)
self.rightFrame.setStyleSheet("background-color:green")
self.rightFrame.setGeometry(QRect(QPoint(150, 0), QSize(150, 300)))
如果你想使用布局,那么QFrame中的setGeometry是没有必要的,你还必须将布局的边距和间距设置为0:
def initGUI(self):
self.leftFrame = QFrame()
self.leftFrame.setStyleSheet("background-color:red")
self.rightFrame = QFrame()
self.rightFrame.setStyleSheet("background-color:green")
self._centralLayout.addWidget(self.leftFrame)
self._centralLayout.addWidget(self.rightFrame)
self._centralLayout.setContentsMargins(0, 0, 0, 0)
self._centralLayout.setSpacing(0)
我只是在试验 PyQt5,所以我可以找出我正在编写的另一个程序中的一些问题,但最重要的是我似乎无法弄清楚如何准确地更改小部件的几何形状。
我用两个不同颜色的帧写了这个小测试 window,我试图让它们拆分整个可能的 space。
class Test(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Test")
self.setGeometry(QRect(QPoint(300, 300), QSize(300, 300)))
self._centralWidget = QWidget()
self._centralLayout = QHBoxLayout()
self.setCentralWidget(self._centralWidget)
self._centralWidget.setLayout(self._centralLayout)
self.initGUI()
self.show()
def initGUI(self):
self.leftFrame = QFrame()
self.leftFrame.setStyleSheet("background-color:red")
self.leftFrame.setGeometry(QRect(QPoint(0, 0), QSize(150, 300))) # Same as QRect(0, 0, 150, 300)
self.rightFrame = QFrame()
self.rightFrame.setStyleSheet("background-color:green")
self.rightFrame.setGeometry(QRect(QPoint(150, 0), QSize(150, 300))) # Same as QRect(150, 0, 150, 300)
self._centralLayout.addWidget(self.leftFrame)
self._centralLayout.addWidget(self.rightFrame)
MainWindow是300x300,所以我把两个frame设置为150x300。但是,当我 运行 它时,有一个令人讨厌的 9x9 偏移量。所以两个框架的真实大小是: left frame: QRect(9, 9, 138, 282)
|右框:QRect(153, 9, 138, 282)
.
我怀疑可能是 QHBoxLayout 弄乱了大小,但我不确定。任何建议表示赞赏!
如果您想手动设置几何图形,那么您不应该使用布局,因为布局的 objective 是根据其特性自动处理它们。
def initGUI(self):
self.leftFrame = QFrame(self._centralWidget)
self.leftFrame.setStyleSheet("background-color:red")
self.leftFrame.setGeometry(QRect(QPoint(0, 0), QSize(150, 300)))
self.rightFrame = QFrame(self._centralWidget)
self.rightFrame.setStyleSheet("background-color:green")
self.rightFrame.setGeometry(QRect(QPoint(150, 0), QSize(150, 300)))
如果你想使用布局,那么QFrame中的setGeometry是没有必要的,你还必须将布局的边距和间距设置为0:
def initGUI(self):
self.leftFrame = QFrame()
self.leftFrame.setStyleSheet("background-color:red")
self.rightFrame = QFrame()
self.rightFrame.setStyleSheet("background-color:green")
self._centralLayout.addWidget(self.leftFrame)
self._centralLayout.addWidget(self.rightFrame)
self._centralLayout.setContentsMargins(0, 0, 0, 0)
self._centralLayout.setSpacing(0)