2014-10-31 67 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