从字典和列表框中删除项目
Delete item from dictionary and Listbox
我有一个程序可以将字典的内容放入 Tkinter Listbox
,但我无法从 Listbox
和字典中删除它。
from tkinter import *
import ast
f = open("orders.txt", "r")
contents = f.read()
f.close()
things = ast.literal_eval(contents)
secondthing = [things, "test"]
root = Tk()
f = Frame(root).pack()
l = Listbox(root)
b = Button(root, text = "delete selection", command = lambda: delete(l))
b.pack()
l.pack()
for i, j in things.items():
oneitem = i + " " + j
l.insert(END, oneitem)
def delete(listbox):
global things
# Delete from Listbox
selection = l.curselection()
l.delete(selection[0])
# Delete from list that provided it
evaluater = l.get(selection[0])
value = eval(evaluater)
ind = things.index(value)
del(things[ind])
print(things)
root.mainloop()
当我尝试删除某些内容时,它会给我:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/mimmo/black_market/aaa.py", line 12, in <lambda>
b = Button(root, text = "delete selection", command = lambda: delete(l))
File "/Users/mimmo/black_market/aaa.py", line 28, in delete
value = eval(evaluater)
File "<string>", line 1
ohhh ohhhhh
^
SyntaxError: unexpected EOF while parsing
谁能帮助我,因为我可以从 Listbox
中删除它,我只是在从字典中删除它时出错。
orders.txt
的内容:
{"ayyy" : "ayyyyyy", "ohhh" : "ohhhhh"}
首先,我建议使用 json
或 pickle
来存储字典的内容——这是最常见的做法。我真的不明白你想做什么,所以我写了一个函数,它通过它的索引从列表框和事物中删除一个元素。
您收到的错误是由 eval
函数引起的,该函数试图将您的列表框项目解释为 python 代码。当然,它会出现语法错误。
# Deletes element from listbox and thigs by it's index
def delete(listbox, index: int):
global things
item = listbox.get(index)
key = item.split()[0]
del things[key]
listbox.delete(index)
我有一个程序可以将字典的内容放入 Tkinter Listbox
,但我无法从 Listbox
和字典中删除它。
from tkinter import *
import ast
f = open("orders.txt", "r")
contents = f.read()
f.close()
things = ast.literal_eval(contents)
secondthing = [things, "test"]
root = Tk()
f = Frame(root).pack()
l = Listbox(root)
b = Button(root, text = "delete selection", command = lambda: delete(l))
b.pack()
l.pack()
for i, j in things.items():
oneitem = i + " " + j
l.insert(END, oneitem)
def delete(listbox):
global things
# Delete from Listbox
selection = l.curselection()
l.delete(selection[0])
# Delete from list that provided it
evaluater = l.get(selection[0])
value = eval(evaluater)
ind = things.index(value)
del(things[ind])
print(things)
root.mainloop()
当我尝试删除某些内容时,它会给我:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1883, in __call__
return self.func(*args)
File "/Users/mimmo/black_market/aaa.py", line 12, in <lambda>
b = Button(root, text = "delete selection", command = lambda: delete(l))
File "/Users/mimmo/black_market/aaa.py", line 28, in delete
value = eval(evaluater)
File "<string>", line 1
ohhh ohhhhh
^
SyntaxError: unexpected EOF while parsing
谁能帮助我,因为我可以从 Listbox
中删除它,我只是在从字典中删除它时出错。
orders.txt
的内容:
{"ayyy" : "ayyyyyy", "ohhh" : "ohhhhh"}
首先,我建议使用 json
或 pickle
来存储字典的内容——这是最常见的做法。我真的不明白你想做什么,所以我写了一个函数,它通过它的索引从列表框和事物中删除一个元素。
您收到的错误是由 eval
函数引起的,该函数试图将您的列表框项目解释为 python 代码。当然,它会出现语法错误。
# Deletes element from listbox and thigs by it's index
def delete(listbox, index: int):
global things
item = listbox.get(index)
key = item.split()[0]
del things[key]
listbox.delete(index)