2014-01-09 78 views
0

我是PySide的新手,我想創建一個多窗口應用程序。例如,第一個窗口將是登錄窗口,如果登錄是正確的,那麼應該隱藏登錄窗口並出現下一個窗口。在PySide中創建多窗口應用程序

我該如何做到這一點?

import sys 
from PySide.QtGui import * 
from PySide.QtCore import * 
import chooseoption 

class Form(QDialog): 
def __init__(self, parent = None): 
    super(Form,self).__init__(parent) 

    self.usernamelabel = QLabel("Username : ") 
    self.passwordlabel = QLabel("Password : ") 
    self.username = QLineEdit() 
    self.password = QLineEdit() 
    self.okbutton = QPushButton("Login") 
    self.username.setPlaceholderText("Enter Username Here") 
    self.password.setPlaceholderText("Enter Password Here") 

    layout = QGridLayout() 
    layout.addWidget(self.usernamelabel,0,0) 
    layout.addWidget(self.passwordlabel,1,0) 
    layout.addWidget(self.username,0,1) 
    layout.addWidget(self.password,1,1) 
    layout.addWidget(self.okbutton) 
    self.setLayout(layout) 

    self.usernamelist = ['priyank','stupendo','ayaan'] 
    self.passwordlist = ['priyank','stupendo','ayaan'] 

    self.connect(self.okbutton, SIGNAL("clicked()"),self.loginfunction) 

def loginfunction(self): 
    usernamestatus = False 
    usernameindex = -1 
    passwordstatus = False 
    passwordindex = -1 
    for currentusername in range(len(self.usernamelist)): 
     if self.passwordlist[currentusername] == self.username.text(): 
      usernamestatus = True 
      usernameindex = self.usernamelist.index(self.passwordlist[currentusername]) 

    for currentpassword in range(len(self.passwordlist)): 
     if self.usernamelist[currentpassword] ==self.password.text(): 
      passwordstatus = True 
      passwordindex = self.passwordlist.index(self.usernamelist[currentpassword]) 

    if usernamestatus == True and passwordstatus ==True and usernameindex == passwordindex: 

     w2 = chooseoption.Form1() 
     w2.show() 

    else: 
     self.msgBox = QMessageBox() 
     self.msgBox.setText("invalid!!!") 
     self.msgBox.exec_() 



app = QApplication(sys.argv) 
form = Form() 
form.show() 
sys.exit(app.exec_()) 

這是我chooseoption.py文件:

import sys 
from PySide.QtGui import * 
from PySide.QtCore import * 

class Form1(QDialog): 

def __init__(self, parent = None): 
    super(Form1,self).__init__(parent) 

    self.addbutton = QPushButton("Add file in Important list") 
    self.removebutton = QPushButton("Remove file from Important list") 
    self.changeaddressbutton = QPushButton("Change Location of Important File") 

    layout = QHBoxLayout() 
    layout.addWidget(self.addbutton) 
    layout.addWidget(self.removebutton) 
    layout.addWidget(self.changeaddressbutton) 
    self.setLayout(layout) 

的問題,這是我的第二個窗口,只是在屏幕上出現幾毫秒,然後消失。我該如何解決這個問題?下面

+2

親愛SO用戶。您未能滿足本網站的要求。你沒有顯示努力或代碼。換句話說,你到目前爲止做了什麼? – Oybek

回答

2

代碼創建三個窗口依次爲:

from PySide.QtCore import * 
from PySide.QtGui import * 
import sys 

class W1(QWidget): 
    def __init__(self, parent=None): 
     super(W1, self).__init__(parent) 
     self.btn = QPushButton('Click1') 

     vb = QVBoxLayout() 
     vb.addWidget(self.btn) 
     self.setLayout(vb) 

     self.btn.clicked.connect(self.fireupWindows2) 

    def fireupWindows2(self): 
     w2 = W2() 
     if w2.exec_(): 
      self.w3 = W3()  
      self.w3.show() 

class W2(QDialog): 
    def __init__(self, parent=None): 
     super(W2, self).__init__(parent) 

     self.btn = QPushButton('Click2') 

     vb = QVBoxLayout() 
     vb.addWidget(self.btn) 
     self.setLayout(vb) 

     self.btn.clicked.connect(self.fireupWindows3) 

    def fireupWindows3(self): 
     self.accept() 

class W3(QWidget): 
    def __init__(self, parent=None): 
     super(W3, self).__init__(parent) 
     self.resize(300, 300) 
     self.btn = QLabel('The Last Window') 

     vb = QVBoxLayout() 
     vb.addWidget(self.btn) 
     self.setLayout(vb) 

if __name__ == "__main__": 
    app = QApplication(sys.argv) 
    w = W1() 
    w.show() 
    sys.exit(app.exec_()) 
+0

你能解釋你爲什麼要做W2級(QDialog)而不是W2級(QWidget)嗎? – user2788859

+0

什麼是exec_()函數? – user2788859

+1

@ user2788859 QDialog繼承了QWidget,只是爲了更具體地使它成爲一個對話框。關於'exec _()',請嘗試刪除該語句,看看會發生什麼;) – zhangxaochen

0

不知怎的,我是能夠解決上述問題

test.py文件:

import sys 
from PySide.QtGui import * 
from PySide.QtCore import * 
import chooseoption 

class Form(QDialog): 
def __init__(self, parent = None): 
    super(Form,self).__init__(parent) 

    self.usernamelabel = QLabel("Username : ") 
    self.passwordlabel = QLabel("Password : ") 
    self.username = QLineEdit() 
    self.password = QLineEdit() 
    self.okbutton = QPushButton("Login") 
    self.username.setPlaceholderText("Enter Username Here") 
    self.password.setPlaceholderText("Enter Password Here") 

    layout = QGridLayout() 
    layout.addWidget(self.usernamelabel,0,0) 
    layout.addWidget(self.passwordlabel,1,0) 
    layout.addWidget(self.username,0,1) 
    layout.addWidget(self.password,1,1) 
    layout.addWidget(self.okbutton) 
    self.setLayout(layout) 

    self.usernamelist = ['priyank','stupendo','ayaan'] 
    self.passwordlist = ['priyank','stupendo','ayaan'] 

    self.connect(self.okbutton, SIGNAL("clicked()"),self.loginfunction) 

def loginfunction(self): 
    usernamestatus = False 
    usernameindex = -1 
    passwordstatus = False 
    passwordindex = -1 
    for currentusername in range(len(self.usernamelist)): 
     if self.passwordlist[currentusername] == self.username.text(): 
      usernamestatus = True 
      usernameindex = self.usernamelist.index(self.passwordlist[currentusername]) 

    for currentpassword in range(len(self.passwordlist)): 
     if self.usernamelist[currentpassword] ==self.password.text(): 
      passwordstatus = True 
      passwordindex = self.passwordlist.index(self.usernamelist[currentpassword]) 

    if usernamestatus == True and passwordstatus ==True and usernameindex == passwordindex: 
     self.hide() 
     w2 = chooseoption.Form1(self) 
     w2.show() 


    else: 
     self.msgBox = QMessageBox() 
     self.msgBox.setText("Bloody Hacker!!!") 
     self.msgBox.exec_() 



app = QApplication(sys.argv) 
form = Form() 
form.show() 
sys.exit(app.exec_()) 

,這是第二個窗口:

import sys 
from PySide.QtGui import * 
from PySide.QtCore import * 

class Form1(QDialog): 

def __init__(self, parent = None): 
    super(Form1,self).__init__(parent) 

    self.addbutton = QPushButton("Add file in Important list") 
    self.removebutton = QPushButton("Remove file from Important list") 
    self.changeaddressbutton = QPushButton("Change Location of Important File") 

    layout = QVBoxLayout() 
    layout.addWidget(self.addbutton) 
    layout.addWidget(self.removebutton) 
    layout.addWidget(self.changeaddressbutton) 
    self.setLayout(layout) 

重要的部分是隱藏第一窗口,並創建具有自作爲參數第二窗口的對象,然後顯示第二窗口

self.hide() 
w2 = chooseoption.Form1(self) 
w2.show()