简体   繁体   中英

Pyqt4 changing splitter layouts on button press

I used two splitters to spilt the window into three sections, there are three buttons on the top splitter and I want the the layout in the bottom splitter to change everytime the button is pressed, so for example when add user is pressed the layout of the bottom splitter should show a form, and when delete is pressed it should bring up a list and so on.

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):      

        vbox = QtGui.QVBoxLayout(self)

        topleft = QtGui.QFrame(self)
        topleft.setFrameShape(QtGui.QFrame.StyledPanel)

        bottomleft = QtGui.QFrame(self)
        bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)

        right = QtGui.QFrame(self)
        right.setFrameShape(QtGui.QFrame.StyledPanel)

        splitterV = QtGui.QSplitter(QtCore.Qt.Vertical)
        splitterV.addWidget(topleft)
        splitterV.addWidget(bottomleft)

        splitterH = QtGui.QSplitter(QtCore.Qt.Horizontal)
        splitterH.addWidget(splitterV)
        splitterH.addWidget(right)

        vbox.addWidget(splitterH)
        self.setLayout(vbox)
        splitterV.setSizes([100,620])
        splitterH.setSizes([960,360])

        ##Layout for the top left of the splitter        
        addUserButton = QtGui.QPushButton('Add New User')
        addUserButton.clicked.connect(self.addNewUser)
        deleteUserButton = QtGui.QPushButton('Delete User')
        deleteUserButton.clicked.connect(self.deleteUser)
        editUserButton = QtGui.QPushButton('Edit User')
        editUserButton.clicked.connect(self.editUser)
        topLayout = QtGui.QHBoxLayout()
        topLayout.addWidget(addUserButton)
        topLayout.addWidget(deleteUserButton)
        topLayout.addWidget(editUserButton)
        topleft.setLayout(topLayout)

        QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))      

    def addNewUser(self):
        ##Layout for the bottom left of the splitter when button pressed
        lbl1 = QtGui.QLabel('First Name')
        lbl2 = QtGui.QLabel('Last Name')

        fname = QtGui.QLineEdit()
        lname = QtGui.QLineEdit()


        grid = QtGui.QGridLayout()
        grid.setSpacing(10)

        grid.addWidget(lbl1, 1, 0)
        grid.addWidget(fname, 1, 1)
        grid.addWidget(lbl2, 2, 0)
        grid.addWidget(lname, 2, 1)

        self.bottomleft.setLayout(grid)

    def deleteUser(self):
        print('Delete')
    def editUser(self):
        print('Edit')

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

I tried many different ways but still cant get it to work any ideas? I'm stuck right now with this error when add user button is pressed:

Traceback (most recent call last):
  File "C:\Users\Program\Test\SettingsTab.py", line 81, in addNewUser
    self.bottomleft.setLayout(grid)
AttributeError: 'Window' object has no attribute 'bottomleft'

I'm not sure how to access the bottom left layout of the window and change it for every key press because i cant access it using self, if it isn't an attribute? then what is it?

It's actually the initUI method that's causing the problem here - you haven't defined the variables as class variables, they're specific to the method, so the addNewUser method can't see them.

topleft = QtGui.QFrame(self)
topleft.setFrameShape(QtGui.QFrame.StyledPanel)

bottomleft = QtGui.QFrame(self)
bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)

right = QtGui.QFrame(self)
right.setFrameShape(QtGui.QFrame.StyledPanel)

should read:

self.topleft = QtGui.QFrame(self)
self.topleft.setFrameShape(QtGui.QFrame.StyledPanel)

self.bottomleft = QtGui.QFrame(self)
self.bottomleft.setFrameShape(QtGui.QFrame.StyledPanel)

self.right = QtGui.QFrame(self)
self.right.setFrameShape(QtGui.QFrame.StyledPanel)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM