Connecting two different widgets together on qt with python through a button

为君一笑 提交于 2019-12-02 14:01:27

You could do the following:

In your QApp create first a dialogue containing the login widget and execute the dialogue. Depending on the result, either (if login failed) quit the application (or reprompt the user for login), or (if login succeeded) instantiate the main window and show it.

Or: Instantiate and show the main window. Immediately show an application-modal dialogue with the login widget. Depending on the result, either resume operation or quit the application.

Here a snippet from some code, that prompts the user for login and checks it against a DB. The login dialogue is defined as DlgLogin in another file.

#many imports
from theFileContainingTheLoginDialogue import DlgLogin

class MainWindow (QtGui.QMainWindow):
    def __init__ (self, parent = None):
        super (MainWindow, self).__init__ ()
        self.ui = Ui_MainWindow ()
        self.ui.setupUi (self)
        dlg = DlgLogin (self)
        if (dlg.exec_ () == DlgLogin.Accepted):
            #check here whether you accept or reject the credentials
            self.database = Database (*dlg.values)
            if not self.database.check (): sys.exit (0)
        else:
            sys.exit (0)
        self.mode = None

The dialogue class is the following (having two line-edit-widgets for the credentials):

from PyQt4 import QtGui
from dlgLoginUi import Ui_dlgLogin

class DlgLogin (QtGui.QDialog):
    def __init__ (self, parent = None):
        super (DlgLogin, self).__init__ ()
        self.ui = Ui_dlgLogin ()
        self.ui.setupUi (self)

    @property
    def values (self):
        return [self.ui.edtUser.text (), self.ui.edtPassword.text () ]

The application itself reads:

#! /usr/bin/python3.3

import sys
from PyQt4 import QtGui
from mainWindow import MainWindow

def main():
    app = QtGui.QApplication (sys.argv)
    m = MainWindow ()
    m.show ()
    sys.exit (app.exec_ () )

if __name__ == '__main__':
    main ()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!