2014-10-31 66 views
1

我是PyQt4的新手。我的问题很简单:我无法打开密码窗口,并且在进行身份验证时关闭/隐藏它,然后打开一个新的单独窗口。第二个窗口消失得很快。我的方法一直是这样(简化):在PyQt4上显示连续的窗口

import sys, time 
from PyQt4 import QtGui 

class Window2(QtGui.QWidget): 
    def __init__(self): 
     super(Window2, self).__init__() 
     self.initUI() 

    def initUI(self): 
     self.setWindowTitle('Window2') 
     # ...add the widgets, etc. 
     self.show() 

class PasswordWindow(QtGui.QWidget): 
    def __init__(self): 
     super(PasswordWindow, self).__init__() 
     self.initUI() 

    def initUI(self): 
     self.setWindowTitle('PasswordWindow') 
     self.show() 
     # ...Here, I'd input the password, authenticate, etc 
     self.hide() 
     w2 = window2() # go to the true main window 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    pw = PasswordWindow() 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 

在此先感谢!

回答

0

我认为它与Window2对象是PasswordWindow.initUI函数的本地对象有关。当我更换

w2 = Window2() 

self.w2 = Window2() 

我得到想要的效果。

+0

谢谢,迈克,这工作,这是一个范围问题。 – 2014-10-31 13:23:21

0

要清楚,当initUi返回时w2会被破坏。做self.w2 = Window2()将w2分配给密码窗口对象,因此w2将存在,直到密码对象被销毁。

+0

谢谢你,Shrewmouse,我试图让程序变得最简单,我可以隐藏密码窗口并让它成为“父”对象,虽然这可能不是很漂亮的代码。 – 2014-11-02 11:53:17