因此,我正在为我的python类构建一个“ TeacherPortal”,老师希望我们通过单击按钮(特别是使用PyQt4)进入另一个窗口。我环顾四周,但只发现了PyQt5,对GUI来说仍然很新
我尝试为主窗口创建2个不同的类,为第二个窗口创建另一个类(它们是单独的类),然后我将一个按钮与另一个类链接在一起,但是它不起作用
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'
答案 0 :(得分: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()