Skip to content
Advertisement

Trying to split a main window into two in Python

I’m learning Python by trying to build a simple plotting application and would like to have two windows in my main screen. I’m using the QSplitter module but am having no luck so far. I’d like the left portion to be used for inputting information via lineEdit() (for now) and the right to show the plot. So far I have

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFrame,QHBoxLayout,QSplitter
from PyQt5.QtWidgets import QMenu, QMenuBar, QLineEdit


class Window(QtWidgets.QMainWindow):
# Main window
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowIcon(QtGui.QIcon("honeywellIcon.png"))
        self.setWindowTitle("Materials Database")
        self.showMaximized()
        self._createMenuBar()
        
        # Splitting the main window
        hbox = QHBoxLayout()
        
        leftFrame = QFrame()
        leftFrame.setFrameShape(QFrame.StyledPanel)
        
        rightFrame =  QFrame()
        rightFrame.setFrameShape(QFrame.StyledPanel)
        
        mainSplitLeft = QSplitter(Qt.Horizontal) 
        
        lineEdit = QLineEdit()
        
        mainSplitLeft.addWidget(leftFrame)
        mainSplitLeft.addWidget(lineEdit)
        mainSplitLeft.setSizes([200,500])
        
        
        mainSplitRight = QSplitter()
        mainSplitRight.addWidget(mainSplitLeft)
        mainSplitRight.addWidget(rightFrame)
        
        hbox.addWidget(mainSplitRight)
        self.setLayout(hbox)
      
        
        self.show()
        
# Menu bars on main window        
    def _createMenuBar(self):
        
        menuBar = self.menuBar()
        
        # Creating 'File' menu option and adding to menuBar
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu) 
        
        # Creating 'Options' menu option and adding to menuBar
        optionsMenu = menuBar.addMenu("&Options")
        menuBar.addMenu(optionsMenu)
        
        # Creating 'View' menu option and adding to menuBar
        viewMenu = menuBar.addMenu("&View")
        menuBar.addMenu(viewMenu)
        
        # Creating 'Create' menu option and adding to menuBar
        createMenu = menuBar.addMenu("&Create")
        menuBar.addMenu(createMenu)
        
        # Creating 'Help' menu option and adding to menuBar
        helpMenu = menuBar.addMenu("&Help")
        menuBar.addMenu(helpMenu)
        

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

This outputs a window with the menu bars but not the split like I had hoped:

Output of above code

How can I achieve a split window?

Advertisement

Answer

My understanding is that a QMainWindow needs to have a centralWidget and you set the layout on the centralWidget. Your QMainWindow should only have one splitter.

Try this:

import sys

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFrame,QHBoxLayout,QSplitter
from PyQt5.QtWidgets import QMenu, QMenuBar, QLineEdit


class Window(QtWidgets.QMainWindow):
# Main window
    def __init__(self, parent=None):
        super().__init__(parent)
        # self.setWindowIcon(QtGui.QIcon("honeywellIcon.png"))
        self.setWindowTitle("Materials Database")
        # self.showMaximized()
        self._createMenuBar()
        self.setCentralWidget(QWidget())

        hbox = QHBoxLayout()

        rightFrame =  QFrame()
        rightFrame.setFrameShape(QFrame.StyledPanel)

        mainSplit = QSplitter(Qt.Horizontal)

        lineEdit = QLineEdit()

        mainSplit.addWidget(lineEdit)
        #mainSplitLeft.setSizes([200,500])


        mainSplit.addWidget(rightFrame)

        hbox.addWidget(mainSplit)
        self.centralWidget().setLayout(hbox)


        self.show()

# Menu bars on main window
    def _createMenuBar(self):

        menuBar = self.menuBar()

        # Creating 'File' menu option and adding to menuBar
        fileMenu = QMenu("&File", self)
        menuBar.addMenu(fileMenu)

        # Creating 'Options' menu option and adding to menuBar
        optionsMenu = menuBar.addMenu("&Options")
        menuBar.addMenu(optionsMenu)

        # Creating 'View' menu option and adding to menuBar
        viewMenu = menuBar.addMenu("&View")
        menuBar.addMenu(viewMenu)

        # Creating 'Create' menu option and adding to menuBar
        createMenu = menuBar.addMenu("&Create")
        menuBar.addMenu(createMenu)

        # Creating 'Help' menu option and adding to menuBar
        helpMenu = menuBar.addMenu("&Help")
        menuBar.addMenu(helpMenu)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window()
    win.show()
    sys.exit(app.exec_())

If your window layout gets more complicated than this, you might consider doing the layout in Qt Designer.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement