user2788859
user2788859

Reputation: 67

Creating a multi-window application in PySide

I am new to PySide, I want to create a multi-window application. For example, the first window will be login window, and if the login is correct then the login window should hide and the next window should appear.

How can I achieve this?

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

This is my chooseoption.py file:

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)

The problem with this is that my second window just appears on the screen for a few milliseconds, and then disappears. How can I fix that?

Upvotes: 0

Views: 5409

Answers (2)

user2788859
user2788859

Reputation: 67

Somehow I was able to solve above problem

test.py file:

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

and this is a second window :

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)

the important part is to hide the first window and create a object of second window with self as a parameter and then show second window

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

Upvotes: 0

zhangxaochen
zhangxaochen

Reputation: 33997

code below creates three windows successively:

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

Upvotes: 2

Related Questions