[英]str' object has no attribute 'get' python
I am coding for use on a raspberry pi. 我正在编码用于树莓派。 I have something like the following dictionary,
我有类似以下字典的内容,
self.appData = {"v": tk.StringVar(), "k": tk.StringVar()}
Then I set a default value 然后我设置一个默认值
self.appData["v"].set("G")
Then I have everything setup for keypad input. 然后,我进行了键盘输入的所有设置。
In my function that deciphers the key that was pushed, I take the keypad push and it suppose to take the string from the current v (self.appData["v"].get()) and then add the new keypad push to it. 在解密密钥的函数中,我进行了小键盘推,并且假设是从当前v(self.appData [“ v”]。get())中提取字符串,然后向其添加新的小键盘推。
Ex: 1st keypad push is "a", The function that deciphers the keypad button pushed, I have something like the following: 例如:第一次按键盘是“ a”,解密按键盘的功能,我有类似以下内容:
x = self.appData["v"].get() + key
self.appData["v"].set(str(x))
so it should get "G" from the dictionary and the "A" to it. 因此它应该从字典中获得“ G”,并从中获得“ A”。 Then I set v to the new x.
然后我将v设置为新的x。 It works fine the first time, then the second time it goes through that function I get the error:
第一次运行正常,然后第二次通过该函数,我得到了错误:
str' object has no attribute 'get' python str'对象没有属性'get'python
Here you are setting self.appData["w"] as a string: 在这里,您将self.appData [“ w”]设置为字符串:
self.appData["v"].set("G")
This only works because tk.StringVar has a set method. 这仅适用于tk.StringVar具有set方法。 This is not how you assign a value inside a dict.
这不是在字典中分配值的方式。 In the same way, this line is wrong:
同样,这一行是错误的:
x = self.appData["v"].get() + key
so you are calling the get() method on a string, and strings don't have a get method. 因此,您要在字符串上调用get()方法,而字符串没有get方法。 I guess that what you want to do is:
我想您想做的是:
x = self.appData.get("v") + key
self.appData["v"] = x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.