Michael
Michael

Reputation: 721

Qwidget window disappears

Okay... This has been bugging me for hours. I have a qtmainwindow with a menubar. I've managed to connect an action in tje menubar to an independent Qwidget. But as soon as the Qwidget appears it disappears. I'm using the latest version of pyqt.

Here's the code:

Import sys
from PyQt4  import QtGui,  QtCore

Class Main(QtGui.QtMainWindow) :
         def __init__(self) :
               QtGui.QtMainWindow.__init__(self) 
               self.setGeometry(300,300,240,320) 
               self.show() 

               menubar  = self. menuBar() 

               filemenu = menubar. addMenu('&File') 

               new = QtGui.QAction(QtGui.QIcon('new.png'), 'New', self) 
               new.triggered.connect(self.pop) 
               filemenu.addAction(new) 

      def pop(self) :
            pop = Pop() 

class Pop(QtGui.QWidget) :
         def __init__(self) :
               QtGui.QWidget.__init__(self) 
              self.setGeometry(300,300,240,320>
              self.setWindowTitle('Pop up') 
             self.show() 

Upvotes: 3

Views: 5085

Answers (2)

Dhanashree Desai
Dhanashree Desai

Reputation: 1020

if you implement this code, you will see the window gets created and disappears immediately.

import sys
from PyQt5 import QtGui, QtWidgets,QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow

app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setGeometry(50,50,500,500)
window.setWindowTitle("GUI window")
window.show()

To solve that problem write "sys.exit(app.exec_())" after window.show() and the window will stay on the screen.

Upvotes: 0

qurban
qurban

Reputation: 3945

Update the pop(self) method as:

def pop(self):
    self.window = Pop()

you need to store object of newly created window in a member variable, other wise as soon as the method finishes with the execution, local variables will be destroyed by the Python Garbage Collector.

Upvotes: 8

Related Questions