6

I've just begun using pyqt4. I followed a tutorial (http://zetcode.com/tutorials/pyqt4/) One thing that puzzles me is this part:

def main():
    app = QtGui.QApplication(sys.argv)
    ex = GUI()
    sys.exit(app.exec())

And the reason for this I explain here:

I have made a small program that opens four more windows except for the first main window. So I tried to replicate what I saw worked with main-window and created a class for every new window and tried to do like with the above. Currently it looks like this:

def main2():
    #app = QtGui.QApplication(sys.argv)
    ex2 = Settings()
    sys.exit(app.exec())

As you can see I have modified it. If I left the first line in the function uncommented the program would crash. I tried to do without the sys.exit(app.exec_())-part but that would only make the new window close milliseconds after it showed. This way though, everything runs and works. Only that in the command window, an error message displays. I don't know how to fix this, since I cannot remove the last line, and I dont't know what to replace "app" with.

I know I'm probably doing the new windows wrong from the beginning, but I don't know how to make these windows open from the original window in any other way. I haven't been able to get anything else to work, and this at least runs and works right now. So the only problem is error messages in the prompt, it would be nice to get rid of them :)

Thanks for any help (complicated and easy ones)!

Forgot to mention, I made the classes start like this:

class GUI(QtGui.QMainWindow):
    def __init__(self):
        super(GUI, self).__init__()
        self.initUI()

and

class Settings(QtGui.QWidget):
    def __init__(self):
        super(Settings, self).__init__()
        ...here goes some more...
        self.initUI2()

and I open Settings-window by calling main2()

right_there
  • 175
  • 1
  • 1
  • 11

1 Answers1

7

You must create one and only one QApplication in your program.

Keep in mind that GUI programming is event-driven, you first declare widgets and then run the main loop with app.exec(), when the user quit your application, app.exec() returns.

The QApplication purpose is to handle user events and propagate them to your code with Qt signals. I suggest you check Qt documentation, it's very complete, even if it's targetting C++ programmers.

So for instance, a way to create two widgets would be:

def main():
    app = QtGui.QApplication(sys.argv)

    ex = QtGui.QWidget()
    ex.show()
    ex2 = QtGui.QWidget()
    ex2.show()

    sys.exit(app.exec())
Julien
  • 475
  • 2
  • 8
  • Thank you, that does clear some things. But I cannot show all windows at the same time. First it's the QMainWindow that shall show at startup, and from there it depends what will open, since I can choose different options in the menubar. An help-window, or settings-window for example. – right_there Apr 26 '13 at 11:19
  • 1
    @right_there: you need to create (or at least show) your settings window in response to an event, see the tutorial you followed about events: http://zetcode.com/tutorials/pyqt4/eventsandsignals/ – Julien Apr 26 '13 at 12:10
  • Oh, I've done that! I just don't want to put all the code here, it's like 560 lines. I've got it all working, the window open when I click the buttons. I have a nice GUI with menubar, I have arranged all the buttons in the settingswindow etc. It's just that it throws error messages about what I described in the first post, and I don't know how to change it all. Maybe I should provide some more code here? – right_there Apr 26 '13 at 13:28