第二次实例化 class 时出错
Error when instantiating a class for the second time
我是 python 和 PyQt 的新手,正在使用它开发我的第一个应用程序,但在尝试实例化我再次创建的 class 时遇到了问题。我遇到以下错误:
Traceback (most recent call last):
File "ConfiguradorAnx.py", line 16, in <lambda>
self.ProductInfo.clicked.connect(lambda: self.newWindow(InfoProduct))
TypeError: 'InfoProduct' object is not callable
Aborted
代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys
class StartWindow(QtWidgets.QMainWindow): #This function should inherit the class
#used to make the ui file
def __init__(self):
super(StartWindow,self).__init__() #Calling the QMainWindow constructor
uic.loadUi('Janela_inicial.ui',self)
#defining quit button from generated ui
self.QuitButton = self.findChild(QtWidgets.QPushButton, 'QuitButton')
self.QuitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
#defining product info button
self.ProductInfo = self.findChild(QtWidgets.QPushButton, 'ProductInformation')
self.ProductInfo.clicked.connect(lambda: self.newWindow(InfoProduct))
self.show() #Show the start window
def newWindow(self, _class):
self.newWindow = _class()
del self.newWindow
class InfoProduct(QtWidgets.QMainWindow):
def __init__(self):
super(InfoProduct,self).__init__()
uic.loadUi('informacao_prod.ui',self)
self.QuitButton = self.findChild(QtWidgets.QPushButton, 'pushButton')
self.QuitButton.clicked.connect(lambda: self.destroy())
self.show()
def main():
app = QtWidgets.QApplication(sys.argv) #Creates a instance of Qt application
InitialWindow = StartWindow()
app.exec_() #Start application
if __name__ == '__main__':
main()
我第一次点击 self.ProductInfo
按钮时它起作用了,InfoProduct window 打开了,但是当我关闭 window 并再次点击同一个按钮时,我已经得到了错误。我不知道我想念的是什么,希望你们能帮忙!
干杯!
您在执行时覆盖了 newWindow
函数:
def newWindow(self, _class):
self.newWindow = _class()
这样做的结果是,下次单击按钮时,lambda 将尝试调用 self.newWindow(InfoProduct)
,但此时 self.newWindow
是 [=15= 的实例],这显然是不可调用的。
解决方法很简单(也很重要)对函数和指向实例的变量使用不同的名称:
self.ProductInfo.clicked.connect(lambda: self.createNewWindow(InfoProduct))
def createNewWindow(self, _class):
self.newWindow = _class()
两个小旁注:
- 不需要使用
findChild
,因为 loadUi
已经为小部件创建了 python 个实例属性:您已经可以访问 self.QuitButton
,等等
- 避免对变量和属性使用大写名称。在 Style Guide for Python Code(又名 PEP-8)上阅读有关此内容和其他代码样式建议的更多信息。
我是 python 和 PyQt 的新手,正在使用它开发我的第一个应用程序,但在尝试实例化我再次创建的 class 时遇到了问题。我遇到以下错误:
Traceback (most recent call last):
File "ConfiguradorAnx.py", line 16, in <lambda>
self.ProductInfo.clicked.connect(lambda: self.newWindow(InfoProduct))
TypeError: 'InfoProduct' object is not callable
Aborted
代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys
class StartWindow(QtWidgets.QMainWindow): #This function should inherit the class
#used to make the ui file
def __init__(self):
super(StartWindow,self).__init__() #Calling the QMainWindow constructor
uic.loadUi('Janela_inicial.ui',self)
#defining quit button from generated ui
self.QuitButton = self.findChild(QtWidgets.QPushButton, 'QuitButton')
self.QuitButton.clicked.connect(QtCore.QCoreApplication.instance().quit)
#defining product info button
self.ProductInfo = self.findChild(QtWidgets.QPushButton, 'ProductInformation')
self.ProductInfo.clicked.connect(lambda: self.newWindow(InfoProduct))
self.show() #Show the start window
def newWindow(self, _class):
self.newWindow = _class()
del self.newWindow
class InfoProduct(QtWidgets.QMainWindow):
def __init__(self):
super(InfoProduct,self).__init__()
uic.loadUi('informacao_prod.ui',self)
self.QuitButton = self.findChild(QtWidgets.QPushButton, 'pushButton')
self.QuitButton.clicked.connect(lambda: self.destroy())
self.show()
def main():
app = QtWidgets.QApplication(sys.argv) #Creates a instance of Qt application
InitialWindow = StartWindow()
app.exec_() #Start application
if __name__ == '__main__':
main()
我第一次点击 self.ProductInfo
按钮时它起作用了,InfoProduct window 打开了,但是当我关闭 window 并再次点击同一个按钮时,我已经得到了错误。我不知道我想念的是什么,希望你们能帮忙!
干杯!
您在执行时覆盖了 newWindow
函数:
def newWindow(self, _class):
self.newWindow = _class()
这样做的结果是,下次单击按钮时,lambda 将尝试调用 self.newWindow(InfoProduct)
,但此时 self.newWindow
是 [=15= 的实例],这显然是不可调用的。
解决方法很简单(也很重要)对函数和指向实例的变量使用不同的名称:
self.ProductInfo.clicked.connect(lambda: self.createNewWindow(InfoProduct))
def createNewWindow(self, _class):
self.newWindow = _class()
两个小旁注:
- 不需要使用
findChild
,因为loadUi
已经为小部件创建了 python 个实例属性:您已经可以访问self.QuitButton
,等等 - 避免对变量和属性使用大写名称。在 Style Guide for Python Code(又名 PEP-8)上阅读有关此内容和其他代码样式建议的更多信息。