Posts: 3
Threads: 1
Joined: Jul 2020
Jul-28-2020, 05:36 PM
(This post was last modified: Jul-28-2020, 07:11 PM by Gribouillis.)
Hello everyone
Please help me solve my little problem.
Created an object in canvas tkinter (canvas.create_image(bot['coord'], image = bot['image'], tags = ('bot', bot ['name'], tag1))) and passed the dictionary as a tag (tag1) - {'name': 'bot_28', 'coord': (610, 160), 'health': 0.99, 'live': 0, 'age': 0, 'speed': 0.41, 'orient': 7, 'cargo': [], 'atak': 0.521, 'radar': 25, 'step': 0, 'near': [], 'image': <PIL.ImageTk.PhotoImage object at 0x000001D3269081D0>}. The dictionary in the tag gets the string type.
Now I need to extract it, but I can't convert it back from a string to a dictionary - an error appears ('str' object has no attribute 'keys' - I wanted to see all the dictionary keys). I don't know how to fix it?
Posts: 6,779
Threads: 20
Joined: Feb 2020
Have you tried using json.loads(string)?
Posts: 3
Threads: 1
Joined: Jul 2020
Jul-29-2020, 02:04 AM
(This post was last modified: Jul-29-2020, 02:04 AM by berc.)
...
...
bot['name'] = 'bot_' + str(i)
bot['coord'] = coord
bot['gender'] = random.choice(gender)
bot['healt'] = 0.99
bot['gormon'] = 0.5
bot['live'] = 0
bot['age'] = 0
bot['speed'] = round((random.random()), 2)
bot['orient'] = random.randint(0, 7)
bot['cargo'] = []
bot['atak'] = round((random.random()), 3)
tag1 = bot
bot['canvas'] = canvas2.create_image(bot['coord'], image = bot['image'], tags = ('bot', bot['name'], tag1))
...
...
collide = canvas2.find_overlapping(x1r, y1r, x2r, y2r)
if len(collide) > 1:
i = 0
for i1 in collide:
if str(canvas2.gettags(collide[i])[1]) != str(bot['name']):
vis = canvas2.gettags(collide[i])
print(str(name) + ' 0 vis: '+str(vis[0]))
print(str(name) + ' 1 vis: '+str(vis[1]))
print(str(name) + ' 2 vis: '+str(vis[2]))
if vis[0] == 'bot':
bott = vis[2]
print(bott.keys())
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\berckut\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\berckut\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 749, in callit
func(*args)
File "ai_bot_01.py", line 460, in collide_bot
print(bott.keys())
AttributeError: 'str' object has no attribute 'keys' Yes, I used json, I used ele... It still leaves an error
Posts: 1,838
Threads: 2
Joined: Apr 2017
The error is pretty clear. Why do you expect bott (and hence vis[2] ) to be a dict?
Posts: 3
Threads: 1
Joined: Jul 2020
...
...
bot = {} #[b]in the code snippet did not give this line, but it is[/b]
bot['name'] = 'bot_' + str(i)
bot['coord'] = coord
bot['gender'] = random.choice(gender)
bot['healt'] = 0.99
bot['gormon'] = 0.5
bot['live'] = 0
bot['age'] = 0
bot['speed'] = round((random.random()), 2)
bot['orient'] = random.randint(0, 7)
bot['cargo'] = []
bot['atak'] = round((random.random()), 3)
tag1 = bot
bot['canvas'] = canvas2.create_image(bot['coord'], image = bot['image'], tags = ('bot', bot['name'], tag1))
...
...
collide = canvas2.find_overlapping(x1r, y1r, x2r, y2r)
if len(collide) > 1:
i = 0
for i1 in collide:
if str(canvas2.gettags(collide[i])[1]) != str(bot['name']):
vis = canvas2.gettags(collide[i])
print(str(name) + ' 0 vis: '+str(vis[0]))
print(str(name) + ' 1 vis: '+str(vis[1]))
print(str(name) + ' 2 vis: '+str(vis[2]))
if vis[0] == 'bot':
bott = vis[2]
print(bott.keys()) because initially bot is a dictionary and I pass it to a tag called tag1. vis is a list of all tags retrieved by canvas2.gettags (collide [i]). vis [2] is the third tag in the list of passed tags and with 'print' it is seen as a dictionary (there are key-value pairs)
I solved this problem in a workaround way - I transfer the number of the desired bot to the function (collide_bot (number)), and then, using the bot number from the list of all bots (Bots), I extract the necessary information for a specific bot
|