在PyQt5中通过GUI中的按钮打开另一个窗口

时间:2017-10-16 15:15:46

标签: python pyqt

我是Python和PyQt的新手。我在MATLAB中设计了GUI,因此它是一种令人沮丧的新体验。现在,我有一个主窗口,我想在按钮上打开另一个登录窗口。我还没有决定是否应该将login的值返回到父窗口或其他窗口。但无论哪种方式,我都需要能够通过按钮打开登录窗口。我创建了2个文件,声明GUI一个作为主窗口,另一个作为Dialog(虽然它似乎不是继承自QDialog)。其他2个py文件被分类,分别调用UI并且工作正常。每个都有自己的功能。我粘贴了main.py(mainwindow init和onOpen函数代码)以及logindialog.py init函数代码。请帮助,否则我将不得不回到Matlab,我可以很容易地做到这一点。

import sys
from PyQt5 import QtWidgets, QtGui
from LoginDialog import LoginDialog
import UI_MSLDB_Main
from UI_LoginDialog import Ui_LoginDialog
#import Helper
#import Auth
#import TestFeature01

class Main(QtWidgets.QMainWindow, UI_MSLDB_Main.Ui_MSLDatabase):
    def _init_(self, parent = None):
         QtWidgets.QMainWindow._init_(self)
         self.setupUi(self)
         self.child = LoginDialog(self)
         self.child.setupUi(QtWidgets.QDialog())
#         self.createConnections()
         self.pushButton_OpenPrimarylist.clicked.connect(self.onOpen)

    def onOpen(self):
#        window = QtWidgets.QDialog
#        self.child = LoginDialog(window, Ui_LoginDialog)
#         self.child.show()
#        exec('LoginDialog.py')
        self.child.show

#        self.actionStudy.triggered.connect()


 if __name__ == "__main__":
     app = QtWidgets.QApplication(sys.argv)
#    LoginDialog(QDialog, Ui_LoginDialog)

#    loginDialog = LoginDialog(QDialog, UI_LoginDialog)
#    username = loginDialog.username
#    password =loginDialog.password
#    Helper.dbConnect(username,password)

#    isAuth = False
#    result = -1
#    while not isAuth:
#        result = loginDialog.exec()
#        if result == LoginDialog.Success or result == LoginDialog.Rejected:
#            isAuth = True
#        else:
#            isAuth = False
    result = 1 
    if result == 1:#LoginDialog.Success:
        MSLDatabase = QtWidgets.QMainWindow()
        ui = UI_MSLDB_Main.Ui_MSLDatabase()
        ui.setupUi(MSLDatabase)
        MSLDatabase.show()

    #        w.show()
        app.exec_()
        sys.exit(app.exec_()) 

有很多事我不知道哪些会起作用。评论的代码是我尝试过但失败的代码。 LoginDialog类在下面。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QMessageBox
import Auth
import Helper
from UI_LoginDialog import Ui_LoginDialog

class LoginDialog(QDialog, Ui_LoginDialog):
    Success, Failed, Rejected, username, password = range(0,5)

    def _init_(self):
        QDialog._init_(self)
        self.setupUi(self)
        QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL 
            (_fromUtf8("accepted()")),self.onAccept)
        QtCore.QObject.connect(self.buttonBox, 
            QtCore.SIGNAL(_fromUtf8("rejected()")),
                           self.onReject)

0 个答案:

没有答案