PyQt4:如何通过单击按钮从一个窗口切换到另一个窗口[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

所以我为我的python类构建了一个“TeacherPortal”,老师希望我们通过点击按钮专门使用PyQt4进入另一个窗口。我环顾四周,但我只发现了PyQt5,而且我还是GUI的新手

我已经尝试为主窗口创建两个不同的类,另一个用于第二个窗口(它们是单独的类)并且我将一个按钮与其链接到另一个类但它不起作用

from PyQt4 import QtGui, QtCore
import sys

class Window(QtGui.QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        self.setWindowTitle("TeahcherPortal")
        self.setGeometry(50,50,800,600)

        self.FirstWindow()

    def FirstWindow(self):
        btn = QtGui.QPushButton("Login",self)
        btn.clicked.connect(SecondPage())
        btn.move(400,300)

        self.show()

class SecondPage(QtGui.QMainWindow):
    def __init__(self):
        super(SecondPage,self).__init__()
        self.setGeometry(50,50,800,600)



def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    Page = SecondPage()
    sys.exit(app.exec_())

run()

我预计它会转到另一个窗口,但这不会发生悲伤。但是我发生了什么错误TypeError: connect() slot argument should be a callable or a signal, not 'SecondPage'

python pyqt4
1个回答
0
投票

试试吧:

import sys
# PyQt5
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *

# PyQt4
#from PyQt4.QtCore    import *
#from PyQt4.QtGui     import *

class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        self.setWindowTitle("TeahcherPortal")
        self.setGeometry(50,50,800,600)

        self.FirstWindow()

    def FirstWindow(self):
        btn = QPushButton("Login", self)
        btn.clicked.connect(self.secondPage)    # - (SecondPage())
        btn.move(400,300)

        self.show()

    def secondPage(self):                       # +++
        self.secondWindow = SecondPage()
        self.secondWindow.show()

class SecondPage(QMainWindow):     
    def __init__(self):
        super(SecondPage,self).__init__()
        self.setWindowTitle("SecondPage")
        self.setGeometry(50,50,800,600)


def run():
    app = QApplication(sys.argv)
    GUI = Window()
    Page = SecondPage()
    sys.exit(app.exec_())

run()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.