Anekdotin
Anekdotin

Reputation: 1591

Handling multiple windows in PYQT

My PYQT4 GUI was getting very lengthy, so I split up the pages to a .py file for each page.

I am trying to traverse the pages, using buttons, but now I cant get it to work :)

Here is what I have so far:

mainwindow.py

import windowConvertor


        self.button2 = QtGui.QPushButton('Convertor Page', self)
        self.button2.clicked.connect(self.pageTwo)

    def pageTwo(self):

        self.hide()
        pagetwo = windowConvertor.convertorPage
        pagetwo.show(self)

windowconvertor.py

class convertorPage(QtGui.QWidget):
    def __init__(self,parent = None):
        QtGui.QWidget.__init__(self, parent)

        self.initUI()

    def initUI(self):

        print "YOU MADE IT!!"

Upvotes: 0

Views: 1360

Answers (1)

three_pineapples
three_pineapples

Reputation: 11849

It looks like you don't understand classes, objects, instantiation and what self is.

These lines are completely wrong:

pagetwo = windowConvertor.convertorPage
pagetwo.show(self)

This code gets a reference to the convertorPage class, and stores a reference to it in pagetwo. You then call pagetwo.show which calls the function show in the convertorPage class and passes it a reference to the object for the first page (self since the pageTwo method, presumably resides within the class for the first page).

Instead, you should instantiate the convertorPage class with:

pagetwo = windowConvertor.convertorPage()

This creates an object of type convertorPage and stores it in pagetwo. You can then call show on this object:

pagetwo.show()

Note: When calling a method of an object, a reference to the object is implicitly passed as the first argument. There is no need to specify it explicitly.

Final notes:

  • Please read up on object oriented programming (and object oriented GUIs). Your code shows you don't quite grasp this yet and you will need to wrap your head around it to effectively program with PyQt.

  • There is a further issue with your code. You are not storing a reference to your new window (pagetwo) which will be garbage collected when the pageTwo method finishes running. You need to fix this by either storing it as an instance attribute (self.pagetwo = ...) or having an overarching parent widget which you pass in when instantiating convertorPage.

Upvotes: 2

Related Questions