pyqt的登录窗口

问:

I nearly finished my application, when the customer asked if I could implement some kind of login form on application startup.

So far I have designed the UI, and tinkered about the actual execution. Username and password are irrelevant for now.

我最近完成了程序,而客户要求在程序启动时,实现一个登录窗体。

因此我设计了UI,并且粗略的实现了执行。这里,用户名和密码是不相干的。


Login form is shown. If correct username and password are entered, then main window is shown and working. But, the login form stays active, and if I close it, the main window will also close.

登录窗体显示。如果输入正确的用户名和密码。主窗体显示,而且工作。但是登录窗口仍然显示,如果关闭它,则主窗体也关闭了。


  1. class Login(QtGui.QDialog):  
  2.     def __init__(self,parent=None):  
  3.         QtGui.QWidget.__init__(self,parent)  
  4.         self.ui=Ui_dlgLogovanje()  
  5.         self.ui.setupUi(self)  
  6.   
  7.         QtCore.QObject.connect(self.ui.buttonLogin, QtCore.SIGNAL("clicked()"), self.doLogin)  
  8.   
  9.     def doLogin(self):  
  10.         name = str(self.ui.lineKorisnik.text())  
  11.         passwd = str(self.ui.lineSifra.text())  
  12.         if name == "john" and passwd =="doe":  
  13.             self.runIt()  
  14.         else:  
  15.             QtGui.QMessageBox.warning(self'Greška',  
  16.         "Bad user or password", QtGui.QMessageBox.Ok)             
  17.   
  18.     def runIt(self):  
  19.         myprogram = Window()          
  20.         myprogram.showMaximized() #myprogram is  
  21.   
  22. class Window(QtGui.QMainWindow):  
  23.     def __init__(self,parent=None):  
  24.         QtGui.QWidget.__init__(self,parent)  
  25.         self.ui=Ui_MainWindow()  
  26.         self.ui.setupUi(self)  
  27.   
  28.   
  29. if __name__=="__main__":  
  30.     program = QtGui.QApplication(sys.argv)  
  31.     myprogram = Window()  
  32.     if Login().exec_() == QtGui.QDialog.Accepted:         
  33.         sys.exit(program.exec_())  



答:

A QDialog has its own event loop, so it can be run separately from the main application.
So you just need to check the dialog's return code to decide whether the main application should be run or not.

QDialgo有自己事件循环,所以它可以独立于主程序运行。

所以你需要检查dialog的返回值,以决定是否运行主程序。

  1. from PyQt4 import QtGui  
  2.   
  3. class Login(QtGui.QDialog):  
  4.     def __init__(self):  
  5.         QtGui.QDialog.__init__(self)  
  6.         self.textName = QtGui.QLineEdit(self)  
  7.         self.textPass = QtGui.QLineEdit(self)  
  8.         self.buttonLogin = QtGui.QPushButton('Login'self)  
  9.         self.buttonLogin.clicked.connect(self.handleLogin)  
  10.         layout = QtGui.QVBoxLayout(self)  
  11.         layout.addWidget(self.textName)  
  12.         layout.addWidget(self.textPass)  
  13.         layout.addWidget(self.buttonLogin)  
  14.   
  15.     def handleLogin(self):  
  16.         if (self.textName.text() == 'foo' and  
  17.             self.textPass.text() == 'bar'):  
  18.             self.accept() #关键  
  19.         else:  
  20.             QtGui.QMessageBox.warning(  
  21.                 self'Error''Bad user or password')  
  22.   
  23. class Window(QtGui.QMainWindow):  
  24.     def __init__(self):  
  25.         QtGui.QMainWindow.__init__(self)  
  26.   
  27. if __name__ == '__main__':  
  28.   
  29.     import sys  
  30.     app = QtGui.QApplication(sys.argv)  
  31.   
  32.     if Login().exec_() == QtGui.QDialog.Accepted:  
  33.         window = Window()  
  34.         window.show()  
  35.         sys.exit(app.exec_()) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值