回到上一个窗口

时间:2017-07-14 08:22:23

标签: python-3.x pyqt pyqt4

我是PythonPyQt4的新人 我想问"How to back to previous Window ?"
我在这里有2个文件,文件'login'和'signup'

here file login.py

import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
                        QApplication, QGridLayout

from signup import SignUp


class Login(QWidget):
    def __init__(self):
        super(Login, self).__init__()

        self.setWindowTitle("Login")

        self.login_window()

    def login_window(self):
        self.login_layout = QGridLayout()

        self.login_button = QPushButton("Login")
        self.signup_button = QPushButton("Sign Up")

        self.login_layout.addWidget(self.login_button, 2, 0)
        self.login_layout.addWidget(self.signup_button, 2, 1)

        self.signup_button.clicked.connect(self.signup_show)

        self.setLayout(self.login_layout)
        self.show()

    def signup_show(self):
        self.signupshow = SignUp()
        self.hide()
        self.signupshow.show()

    def check_signup(self):
        SignUp.check_signup()
        self.show()


def main():
    app = QApplication(sys.argv)
    login = Login()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

and here is signup.py

import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
                        QApplication, QGridLayout


class SignUp(QWidget):
    def __init__(self):
        super(SignUp, self).__init__()

        self.setWindowTitle("Sign Up")

        self.signup_window()

    def signup_window(self):

        self.signup_layout = QGridLayout()
        self.signup_button = QPushButton("Sign Up")
        self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
        self.signup_button.clicked.connect(self.check_signup)
        self.setLayout(self.signup_layout)
        self.show()

    def check_signup(self):
        self.close()

def main():
    app = QApplication(sys.argv)
    signup = SignUp()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

我的问题是,当我从signup.py按下按钮signup时,它已关闭,但窗口登录未显示。
我认为我在login.py中的trigger的signup.py中没有任何check_signup 在此代码中,我删除了一些Line EditLabel。我认为这不是问题 我希望有人会帮忙,谢谢你:) 如果你回答这个问题,我希望你能解释一下这个逻辑,再次感谢你:)

2 个答案:

答案 0 :(得分:0)

我不确定你想要实现的目标(你的描述有点令人困惑),但让我们从这里开始吧。我将所有代码放在一个文件中,因为您需要彼此import(这会导致循环导入)。然后,我刚刚添加了这两行

self.login = Login()
self.login.show()

进入check_signup类的SignUp方法,弹出Login窗口。如果这不是您想要的结果,请告诉我们并向我们提供更好的说明。代码如下:

import sys
from PyQt4.QtGui import QWidget, QPushButton, QLineEdit, QLabel, \
                        QApplication, QGridLayout

class Login(QWidget):
    def __init__(self):
        super(Login, self).__init__()

        self.setWindowTitle("Login")

        self.login_window()

    def login_window(self):
        self.login_layout = QGridLayout()

        self.login_button = QPushButton("Login")
        self.signup_button = QPushButton("Sign Up")

        self.login_layout.addWidget(self.login_button, 2, 0)
        self.login_layout.addWidget(self.signup_button, 2, 1)

        self.signup_button.clicked.connect(self.signup_show)

        self.setLayout(self.login_layout)
        self.show()

    def signup_show(self):
        self.signupshow = SignUp()
        self.hide()
        self.signupshow.show()

    def check_signup(self):
        SignUp.check_signup()
        self.show()


class SignUp(QWidget):
    def __init__(self):
        super(SignUp, self).__init__()

        self.setWindowTitle("Sign Up")

        self.signup_window()

    def signup_window(self):

        self.signup_layout = QGridLayout()
        self.signup_button = QPushButton("Sign Up")
        self.signup_layout.addWidget(self.signup_button, 2, 0, 1, 0)
        self.signup_button.clicked.connect(self.check_signup)
        self.setLayout(self.signup_layout)
        self.show()

    def check_signup(self):
        self.login = Login()
        self.login.show()
        self.close()

def main():
    app = QApplication(sys.argv)
    signup = SignUp()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

您应该使用信号槽连接:

import sys
from PyQt4 import QtGui, QtCore


class WidgetA(QtGui.QWidget):
    open_b = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(WidgetA, self).__init__(parent)
        self.resize(100, 100)
        self.l = QtGui.QVBoxLayout()
        self.close_btn = QtGui.QPushButton('Close')
        self.b_btn = QtGui.QPushButton('Open B')
        self.b_btn.clicked.connect(self.b_btn_clicked)
        self.l.addWidget(self.close_btn)
        self.l.addWidget(self.b_btn)
        self.setLayout(self.l)

    def b_btn_clicked(self):
        self.open_b.emit()
        self.hide()


class WidgetB(QtGui.QWidget):
    open_a = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        super(WidgetB, self).__init__(parent)
        self.resize(100, 100)
        self.l = QtGui.QVBoxLayout()
        self.close_btn = QtGui.QPushButton('Close')
        self.close_btn.clicked.connect(self.hide)
        self.a_btn = QtGui.QPushButton('Open A')
        self.a_btn.clicked.connect(self.a_btn_clicked)
        self.l.addWidget(self.close_btn)
        self.l.addWidget(self.a_btn)
        self.setLayout(self.l)

    def a_btn_clicked(self):
        self.open_a.emit()
        self.hide()


def main():
    app = QtGui.QApplication(sys.argv)
    a = WidgetA()
    b = WidgetB()
    a.open_b.connect(lambda: b.show())
    b.open_a.connect(lambda: a.show())
    a.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()