Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyqt5,类函数赢得';不要更改标签文本_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python Pyqt5,类函数赢得';不要更改标签文本

Python Pyqt5,类函数赢得';不要更改标签文本,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,如果你需要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):

如果你需要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(或find)时,弹出窗口显示“Python已停止工作。一个问题导致程序停止正常工作。Windows将关闭该程序并通知您是否有可用的解决方案。”

我猜问题出在这方面

def printInfo(self):
     self.name.setText(self.name)
     self.birth.setText(self.birth)
     self.location.setText(self.location)

因为当我把其中一个或两个都改为“print(“Hi”)”时,它就起作用了


提前谢谢

多亏了musicamate。他说“类被用作数据容器,因此它不能直接修改另一个对象”-“创建一个返回其字段的方法”

所以我照他说的做了,为我的课做了返回函数。 经过深思熟虑,我把这个编码了

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

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

是的,问题是,
self
是一个实例
Users
,所有这些名字、出生、位置和密码都是字符串,没有任何
setText
属性。因为您没有提供一个UI来使用,所以给您一个实际的答案有点困难,但我还是要给您一个提示:您的
Users
类被用作数据容器,因此它不能直接修改另一个对象;您可以创建一个返回其字段的方法,在
find
(如果该键存在)中调用该方法并从中设置值。此外,您不应该像对
stackedWidget
那样从实例访问全局变量。我知道这只是一个例子,但很少有情况下这样做是一个好主意,而且只有当您真正了解自己在做什么时才应该这样做,特别是考虑到您正在尝试访问父对象。您使用了
self.name
作为字符串和标签小部件。不能两者兼而有之。如果你上传你的用户界面代码,我们可以提供更多帮助。并尽量以最少的方式编写代码,但要重复。只要键入正确的名称,提及就足够了;-)
self.name.setText(userDict[self.input.text()].returnName())
self.birth.setText(userDict[self.input.text()].returnBirth())
self.location.setText(userDict[self.input.text()].returnLocation())