[英]Pyqt5, Class function won't change label text
如果你需要用户界面本身,只需评论它,我会直接发送给你
class Users:
def __init__(self, name, birth, location, password):
self.name = name
self.birth = birth
self.location = location
self.password = password
def printInfo(self):
self.name.setText(self.name)
self.birth.setText(self.birth)
self.location.setText(self.location)
userDict = dict()
userDict["Jeff"] = Users("Jeff", "Something", "Something", "12345")
# Class Window For Start Screen
class Start(QDialog):
def __init__(self):
super().__init__()
loadUi("start.ui", self) # Load Pyqt5 Designer Ui and Objects
self.setFixedWidth(588)
self.setFixedHeight(400)
self.startButton.clicked.connect(self.pressStart)
@staticmethod
def pressStart():
stackedWidget.setCurrentIndex(1)
# Class Window for startGame
class startGame(QDialog):
def __init__(self):
super(startGame, self).__init__()
loadUi("game_start.ui", self) # Load PyQt5 Designer Ui and Objects
self.setFixedWidth(588)
self.setFixedHeight(400)
self.menuButt.clicked.connect(self.pressMenu)
self.input.returnPressed.connect(self.find)
self.findData.clicked.connect(self.find)
def find(self):
if self.input.text() in userDict.keys():
userDict[self.input.text()].printInfo()
else:
noData()
@staticmethod
def pressMenu():
stackedWidget.setCurrentIndex(0)
def noData():
warning = QMessageBox()
warning.setWindowTitle("Warning")
warning.setText("No Data Found")
warning.exec_()
app = QApplication(sys.argv)
mainWin = Start()
game = startGame()
stackedWidget = QStackedWidget()
stackedWidget.addWidget(mainWin)
stackedWidget.addWidget(game)
stackedWidget.show()
app.exec()
我正在使用 Pycharm 并且没有显示错误。 但是当我输入“Jeff”并按回车(或查找)时,会出现一个弹出窗口,提示“Python 已停止工作。一个问题导致程序停止正常工作。Windows 将关闭该程序并通知您是否有可用的解决方案。”
我猜问题出在这一行
def printInfo(self):
self.name.setText(self.name)
self.birth.setText(self.birth)
self.location.setText(self.location)
或者
userDict[self.input.text()].printInfo()
因为当我将其中一个或两个更改为“print(“Hi”)”时,它会起作用。
提前致谢!
感谢音乐人。 他说“类被用作数据容器,因此它不应该能够直接修改另一个对象”——“创建一个返回其字段的方法”
所以我按照他说的做了,为我的班级做了返回函数。 我经过深思熟虑,对此进行了编码。
self.name.setText(userDict[self.input.text()].returnName())
self.birth.setText(userDict[self.input.text()].returnBirth())
self.location.setText(userDict[self.input.text()].returnLocation())
非常感谢他。 除了提到他的名字,我不知道我能相信他多少。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.