我是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)
这个问题是我的第二个窗口只在屏幕上显示几毫秒,然后消失。我该如何解决这个问题?
答案 0 :(得分: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_())
答案 1 :(得分: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作为参数,然后显示第二个窗口
self.hide()
w2 = chooseoption.Form1(self)
w2.show()