Qwidget window disappears

2019-02-11 05:32发布

问题:

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() 

回答1:

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.



标签: pyqt4