Qwidget 窗口消失 [英] Qwidget window disappears

查看:105
本文介绍了Qwidget 窗口消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的...这已经困扰了我好几个小时了.我有一个带菜单栏的 qtmainwindow.我已经设法将 tje 菜单栏中的一个动作连接到一个独立的 Qwidget.但是一旦 Qwidget 出现,它就会消失.我正在使用最新版本的 pyqt.

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.

代码如下:

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

推荐答案

更新 pop(self) 方法为:

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

您需要将新创建的窗口的对象存储在一个成员变量中,否则一旦方法执行完成,局部变量将被 Python 垃圾收集器销毁.

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.

这篇关于Qwidget 窗口消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆