我很难读取我的字典变量. Python不断抛出以下错误:
I'm having hard time trying to read my dictionary variable. Python keeps throwing the following error:
TypeError: string indices must be integers这是一个示例,应该使您了解我的问题是什么
This is a sample that should give you an idea of what my problem is:
self.dict = {} self.dict['User'] = 'User_name' self.dict['Dir'] = 'user_home_dir' self.dict['userID'] = 1 if self.dict['userID'] == 1: # this is the line that is said to contain an error then do somethin ...这就是我得到的回溯信息:
This is all I get as a Traceback:
user_id = self.dict['userID'] TypeError: string indices must be integers不,我很肯定self.dict不是字符串.
No I'm positive that self.dict is not a string.
它是在一个名为createUser的函数中创建的,并返回到主线程.
It's being created in a function called createUser and it's being returned to the main thread.
def createUser(self): self.dict = {} ... user inputs information ... ... and then information is inserted into self.dict ... self.dict['User'] = self.lineEdit_User.text() self.dict['Dir'] = self.lineEdit_path.text() self.dict['userID'] = int(self.lineEdit_ID.text()) return self.dict此后,仅引用self.dict以便从中读取数据.
After that self.dict is only referenced for reading data from it.
推荐答案我已经找到问题所在,并且在我的信号定义中.字典'self.dict'通过pyqtSignal返回,其定义如下:
I've found the problem, and it was in my signal definition. Dictionary 'self.dict' is being returned via pyqtSignal which was defined like this:
addUser = pyqtSignal(object)仔细检查我的代码后,我发现应该将其更改为:
After thorough examination of my code I've figured out that I should change it to:
addUser = pyqtSignal(dict)一旦我做到了,那就好了.再次感谢大家的贡献.
and once I did that it was all right. Once again, thanks everyone for their contribution.
更多推荐
从字典中检索信息
发布评论