【问题标题】:Pyqt5, Class function won't change label textPyqt5,类函数不会改变标签文本
【发布时间】:2020-10-29 22:22:30
【问题描述】:

如果你需要 ui 本身,只需评论它,我会直接发送给你

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”并按 Enter(或查找)时,会出现一个弹出窗口,显示“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”)”时,它会起作用。

提前致谢!

【问题讨论】:

  • 是的,问题就在这里,selfUsers 的一个实例,所有这些名称、出生、位置和密码都是字符串,并且没有任何setText 属性。由于您没有提供可使用的 UI,因此很难给您一个实际的答案,但无论如何我都会给您一个提示:您的 Users 类用作数据容器,因此它应该不能直接修改另一个对象;您可以做的是创建一个返回其字段的方法,在find(如果键存在)中调用该方法并从那里设置值。
  • 另外,您应该像为stackedWidget 所做的那样从实例访问全局变量。我知道这只是一个例子,但在极少数情况下,这样做是个好主意,而且只有在你真正知道自己在做什么时才应该这样做,特别是考虑到你正在尝试访问父对象。
  • 您使用self.name 作为字符串和标签小部件。它不能两者兼而有之。如果您上传您的 ui 代码,我们可以提供更多帮助。并尝试始终以最少但可重现的方式放置您的代码。

标签: python python-3.x pyqt pyqt5


【解决方案1】:

感谢音乐家。他说“类被用作数据容器,因此它不应该能够直接修改另一个对象”-“创建一个返回其字段的方法”

所以我照他说的做了,并为我的班级制作了返回函数。 经过深思熟虑,我编写了这个代码。

self.name.setText(userDict[self.input.text()].returnName())
self.birth.setText(userDict[self.input.text()].returnBirth())
self.location.setText(userDict[self.input.text()].returnLocation())

非常感谢他。除了提到他的名字,我不知道我还能相信他多少。

【讨论】:

  • 提及就好了,只要你输入正确的名字;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
  • 2020-04-27
  • 2011-01-19
相关资源
最近更新 更多