强制python pyside分离器启动窗口中心

时间:2015-11-20 22:42:14

标签: python pyside

如何在开始时强制拆分器定位在窗口的中心?正如您在下面的代码中所看到的,由于按钮很小,它有利于右侧。但是我想让分离器总是出现在窗口的中间,如图2所示。

当前

enter image description here

目标

enter image description here

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):      

        # formatting
        self.resize(550, 400)
        self.setWindowTitle("Cameras")

        # widgets
        self.ListA = QtGui.QTreeWidget()
        self.ListB = QtGui.QTreeWidget()
        self.Button = QtGui.QPushButton()

        # layout Splitter
        self.mainLayout = QtGui.QHBoxLayout(self)
        self.mainLayout.setContentsMargins(5,5,5,5)

        self.leftPanel = QtGui.QFrame(self)
        # self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
        self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
        self.leftPanelLayout.setContentsMargins(0,0,0,0)
        self.leftPanelLayout.addWidget(self.ListA)

        self.rightPanel = QtGui.QFrame(self)
        # self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
        self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
        self.rightPanelLayout.setContentsMargins(0,0,0,0)
        self.rightPanelLayout.addWidget(self.Button)

        self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.splitter.addWidget(self.leftPanel)
        self.splitter.addWidget(self.rightPanel)

        self.mainLayout.addWidget(self.splitter)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QtGui.QSplitter')
        self.show()

    def onChanged(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()        


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

的Bam!得到了它。

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):      

        # formatting
        self.resize(1000, 1000)
        self.setWindowTitle("Cameras")

        # widgets
        self.ListA = QtGui.QTreeWidget()
        self.ListB = QtGui.QTreeWidget()
        self.Button = QtGui.QPushButton()

        # layout Splitter
        # QHBoxLayout
        self.mainLayout = QtGui.QGridLayout(self)
        self.mainLayout.setContentsMargins(5,5,5,5)

        self.leftPanel = QtGui.QFrame(self)
        # self.leftPanel.setFrameShape(QtGui.QFrame.StyledPanel)
        self.leftPanelLayout = QtGui.QHBoxLayout(self.leftPanel)
        self.leftPanelLayout.setContentsMargins(0,0,0,0)
        self.leftPanelLayout.addWidget(self.ListA)

        self.rightPanel = QtGui.QFrame(self)
        # self.rightPanel.setFrameShape(QtGui.QFrame.StyledPanel)
        self.rightPanelLayout = QtGui.QHBoxLayout(self.rightPanel)
        self.rightPanelLayout.setContentsMargins(0,0,0,0)
        self.rightPanelLayout.addWidget(self.Button)

        self.splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
        self.splitter.addWidget(self.leftPanel)
        self.splitter.addWidget(self.rightPanel)
        self.splitter.setCollapsible(0,False)
        self.splitter.setCollapsible(1,False)

        self.mainLayout.addWidget(self.splitter,0,0)

        self.setWindowTitle('QtGui.QSplitter')
        self.show()

        self.set_panel_sizes(self.splitter)


    def onChanged(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()      

    def set_panel_sizes(self, ctrl):
        width = ctrl.frameSize().width() / 2.0
        ctrl.setSizes( [width,width] )  


def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()