如何从另一个pyqt5应用程序打开pyqt5应用程序?

时间:2017-11-14 09:04:18

标签: python pyqt pyqt5

文件名:login.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_login(object):
    key=-1
    def overall_coordinator_clicked(self):
                self.overall_coordinator.setStyleSheet("border-bottom:3px solid red;\n"
"border-top:3px solid red;\n")
                self.coordinator.setStyleSheet("border:2px solid white;")
                self.member.setStyleSheet("border:2px solid white;")
                self.volunteer.setStyleSheet("border:2px solid white;")
                key=0
    def coordinator_clicked(self):
                self.overall_coordinator.setStyleSheet("border:2px solid white;")
                self.coordinator.setStyleSheet("border-bottom:3px solid red;\n"
"border-top:3px solid red;\n")
                self.member.setStyleSheet("border:2px solid white;")
                self.volunteer.setStyleSheet("border:2px solid white;")
                key=1
    def member_clicked(self):
                self.overall_coordinator.setStyleSheet("border:2px solid white;")
                self.coordinator.setStyleSheet("border:2px solid white;")
                self.member.setStyleSheet("border-bottom:3px solid red;\n"
"border-top:3px solid red;\n")
                self.volunteer.setStyleSheet("border:2px solid white;")
                key=2
    def volunteer_clicked(self):
                self.overall_coordinator.setStyleSheet("border:2px solid white;")
                self.coordinator.setStyleSheet("border:2px solid white;")
                self.member.setStyleSheet("border:2px solid white;")
                self.volunteer.setStyleSheet("border-bottom:3px solid red;\n"
"border-top:3px solid red;\n")
                key=3
    #def login(self):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(923, 606)
        MainWindow.setMinimumSize(QtCore.QSize(923, 606))
        MainWindow.setMaximumSize(QtCore.QSize(923, 606))
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background:#1A237E;")
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.widget = QtWidgets.QWidget(self.centralWidget)
        self.widget.setGeometry(QtCore.QRect(330, 30, 231, 201))
        self.widget.setStyleSheet("border-image:url(\"techfusion_logo.png\");")
        self.widget.setObjectName("widget")
        self.login_btn = QtWidgets.QPushButton(self.centralWidget)
        self.login_btn.setGeometry(QtCore.QRect(350, 480, 200, 41))
        self.login_btn.setStyleSheet(" color:white;\n"
" background-color:#1A237E;\n"
" border-radius:20px;\n"
"font-weight:bold;\n"
"font-size:18px;\n"
"border : 2px solid white;\n"
"\n"
"")
        self.login_btn.setObjectName("login_btn")
        #self.login_btn.clicked.connect(self.login)
        self.login_password = QtWidgets.QLineEdit(self.centralWidget)
        self.login_password.setGeometry(QtCore.QRect(290, 420, 331, 25))
        self.login_password.setStyleSheet("border: 1px solid transparent;\n"
"border-bottom:2px solid white;\n"
"color:white;\n"
"background-color:transparent;")
        self.login_password.setText("")
        self.login_password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.login_password.setObjectName("login_password")
        self.login_username = QtWidgets.QLineEdit(self.centralWidget)
        self.login_username.setGeometry(QtCore.QRect(290, 370, 331, 25))
        self.login_username.setStyleSheet("border: 1px solid transparent;\n"
"border-bottom:2px solid white;\n"
"background-color:transparent;\n"
"color:white;\n"
"")
        self.login_username.setObjectName("login_username")
        self.designation = QtWidgets.QGroupBox(self.centralWidget)
        self.designation.setGeometry(QtCore.QRect(100, 280, 701, 61))
        self.designation.setStyleSheet("color:white;\n"
"font-weight:bold;\n"
"border:0px solid transparent;")
        self.designation.setAlignment(QtCore.Qt.AlignCenter)
        self.designation.setObjectName("designation")
        self.overall_coordinator = QtWidgets.QPushButton(self.designation)
        self.overall_coordinator.setGeometry(QtCore.QRect(-10, 30, 181, 28))
        self.overall_coordinator.setStyleSheet("border: 2px solid white;\n"
"background-color:transparent;\n"
"font-weight:normal;\n"
"color:white;\n"
"")
        self.overall_coordinator.setObjectName("overall_coordinator")
#######overall_coordinator selected######
        self.overall_coordinator.clicked.connect(self.overall_coordinator_clicked)
#########################################
        self.coordinator = QtWidgets.QPushButton(self.designation)
        self.coordinator.setGeometry(QtCore.QRect(170, 30, 181, 28))
        self.coordinator.setStyleSheet("border: 2px solid white;\n"
"background-color:transparent;\n"
"font-weight:normal;\n"
"color:white;\n"
"")
        self.coordinator.setObjectName("coordinator")
#######overall_coordinator selected######
        self.coordinator.clicked.connect(self.coordinator_clicked)
#########################################
        self.member = QtWidgets.QPushButton(self.designation)
        self.member.setGeometry(QtCore.QRect(350, 30, 181, 28))
        self.member.setStyleSheet("border: 2px solid white;\n"
"background-color:transparent;\n"
"font-weight:normal;\n"
"color:white;\n"
"")
        self.member.setObjectName("member")
#######overall_coordinator selected######
        self.member.clicked.connect(self.member_clicked)
#########################################
        self.volunteer = QtWidgets.QPushButton(self.designation)
        self.volunteer.setGeometry(QtCore.QRect(530, 30, 181, 28))
        self.volunteer.setStyleSheet("border: 2px solid white;\n"
"background-color:transparent;\n"
"font-weight:normal;\n"
"color:white;\n"
"")
        self.volunteer.setObjectName("volunteer")
#######overall_coordinator selected######
        self.volunteer.clicked.connect(self.volunteer_clicked)
#########################################
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 923, 22))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Voyage"))
        self.login_btn.setText(_translate("MainWindow", "LOGIN"))
        self.login_username.setPlaceholderText(_translate("MainWindow", "Username"))
        self.login_password.setPlaceholderText(_translate("MainWindow", "Password"))
        self.designation.setTitle(_translate("MainWindow", "LOGIN AS"))
        self.overall_coordinator.setText(_translate("MainWindow", "Overall Coordinator"))
        self.coordinator.setText(_translate("MainWindow", "Coordinator"))
        self.member.setText(_translate("MainWindow", "Member"))
        self.volunteer.setText(_translate("MainWindow", "Volunteer"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_login()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

文件名:dashboard.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from login import Ui_login


class Ui_dashboard(object):
    #def member(self):
    #def team(self):
    #def log_out(self):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(923, 606)
        MainWindow.setMinimumSize(QtCore.QSize(923, 606))
        MainWindow.setMaximumSize(QtCore.QSize(923, 606))
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background:#1A237E;")
        self.centralWidget = QtWidgets.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.widget = QtWidgets.QWidget(self.centralWidget)
        self.widget.setGeometry(QtCore.QRect(340, 40, 231, 201))
        self.widget.setStyleSheet("border-image:url(\"techfusion_logo.png\");")
        self.widget.setObjectName("widget")
        self.member = QtWidgets.QPushButton(self.centralWidget)
        self.member.setGeometry(QtCore.QRect(360, 320, 200, 41))
        self.member.setStyleSheet(" color:white;\n"
" background-color:#1A237E;\n"
" border-radius:20px;\n"
"border : 2px solid white;\n"
"font-weight:bold;\n"
"\n"
"")
        self.member.setObjectName("member")
        self.team = QtWidgets.QPushButton(self.centralWidget)
        self.team.setGeometry(QtCore.QRect(360, 400, 200, 41))
        self.team.setStyleSheet(" color:white;\n"
" background-color:#1A237E;\n"
" border-radius:20px;\n"
"font-weight:bold;\n"
"border : 2px solid white;\n"
"\n"
"")
        self.team.setObjectName("team")
        self.logout = QtWidgets.QPushButton(self.centralWidget)
        self.logout.setGeometry(QtCore.QRect(820, 10, 89, 25))
        self.logout.setStyleSheet(" color:white;\n"
                                  " background-color:#1A237E;\n"
                                  " border-radius:20px;\n"
                                  "font-weight:bold;\n"
                                  "text-decoration:underline;\n"
                                  "font-size:15px;\n"
                                  "\n"
                                  "")
        self.logout.setObjectName("logout")
        #self.logout.clicked.connect(self.log_out)
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtWidgets.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 923, 22))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.statusBar = QtWidgets.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Voyage"))
        self.member.setText(_translate("MainWindow", "MEMBER"))
        self.team.setText(_translate("MainWindow", "TEAM"))
        self.logout.setText(_translate("MainWindow", "LOGOUT"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_dashboard()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

QPushButton名为" logout"在dashboard.py中。我想要的是当我点击注销按钮时,dashboard.py退出并登录login.py。我应该使用什么代码来实现这一目标。自从过去2天以来我一直坚持这个。通过许多网页,但我无法做到这一点。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

不建议修改Qt Designer生成的设计,创建管理设计和实现逻辑的类是合适的。由于您的目标是关闭当前窗口并按下按钮打开另一个窗口,然后我们必须使用按钮的单击信号并将它们连接到两个任务,如下所示:

.
├── dashboard.py
├── login.py
└── main.py

<强> main.py

from PyQt5.QtWidgets import *
from login import Ui_login
from dashboard import Ui_dashboard


class Login(QMainWindow, Ui_login):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)

class Dashboard(QMainWindow, Ui_dashboard):
    def __init__(self, *args, **kwargs):
        QMainWindow.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.logout.clicked.connect(self.close)

if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    w = Dashboard()
    l = Login()
    w.logout.clicked.connect(l.show)
    w.show()
    sys.exit(app.exec_())