JonnyPython
JonnyPython

Reputation: 121

PySide, open a new window from the running program

Code:

import sys
from PySide import QtGui

class BrowserDevelopment(QtGui.QMainWindow):

    def __init__(self):
        super(BrowserDevelopment, self).__init__()
        self.startingUI()

    def startingUI(self):

        self.setWindowTitle('Alphabrowser')
        self.resize(800, 400)
        self.statusBar()

        #Menueinstellungen an sich

        menue = self.menuBar()

        #Actions des Menues:
        #datei menue
        menuleiste_datei = menue.addMenu('File')

        datei_exit = QtGui.QAction('Exit', self)
        datei_exit.setStatusTip('Close the programm')
        menuleiste_datei.addAction(datei_exit)
        datei_exit.triggered.connect(self.close)

        #Einstellungen menue
        menuleiste_configurations = menue.addMenu('Configurations')

        configurations_settings = QtGui.QAction('Settings', self)
        configurations_settings.setStatusTip('Configurations(Settings)')
        menuleiste_configurations.addAction(configurations_settings)
        configurations_settings.triggered.connect(self.newwindow)

        self.show()

    def newwindow(self):
        wid = QtGui.QWidget()
        wid.resize(250, 150)
        wid.setWindowTitle('NewWindow')
        wid.show()


def main():

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

if __name__== '__main__':
    main()

If you click "Configurations" and then "Settings" a window should pop up, which it does. But it flashes and vanishes. I tried to add a second sys.exit(app.exec_()) but it's not defined since its in another method. Should i just make app global or has this something to do with the so called "threading"? Greets

Upvotes: 0

Views: 5727

Answers (1)

insys
insys

Reputation: 1288

You need to keep a reference of your new window. Otherwise, it is unreferenced and will be garbage collected.

    def newwindow(self):
        self.wid = QtGui.QWidget()
        self.wid.resize(250, 150)
        self.wid.setWindowTitle('NewWindow')
        self.wid.show()

Although I would not create the reference in the newwindow method. You could better set up your preferences window in your UI initialization method and then just call self.wid.show()

Upvotes: 3

Related Questions