从字典中检索信息

编程入门 行业动态 更新时间:2024-10-10 23:20:04
本文介绍了从字典中检索信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难读取我的字典变量. 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.

更多推荐

从字典中检索信息

本文发布于:2023-11-06 08:32:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   信息

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!