英文:
Python dictionary, list and for-loop bug
问题
我正在尝试在一个tkinter窗口中显示来自数据集的元素列表。我希望能够通过高亮、删除等方式操纵这些元素。
我有这段代码:
from tkinter import *
window = Tk()
window.geometry("100x100")
# 从API获取的数据
data_list = [
["1", "Lorem"],
["2", "Lorem"],
["3", "Lorem"],
["4", "Lorem"]
]
# 使用删除按钮创建可选择的矩形
rectangles = {}
delete_buttons = {}
def CreateRectangles():
i = 0
for data in data_list:
rectangles[i] = Canvas(window, bg="#BFBFBF", height=15, width=80)
rectangles[i].place(x=19, y=20.0 + (i * 19))
rectangles[i].create_text(5.0, 1.0, anchor="nw", text=str(f'#{data[0]}:{data[1]}'))
delete_buttons[i] = Label(window, text="X ", bg="#D9D9D9")
delete_buttons[i].place(x=6, y=20.0 + (i * 19))
i += 1
CreateRectangles()
# 突出显示单击的矩形
def RectangleClick(e, arg):
# 重置所有矩形的外观
for i in rectangles:
rectangles[i].config(bg="#BFBFBF")
# 突出显示被单击的矩形
rectangles[arg].config(bg="#999999")
for key in rectangles:
rectangles[key].bind("<ButtonPress-1>", lambda event, arg=key: RectangleClick(event, arg))
# 删除按钮操作
def DeleteClick(e, arg):
# 从窗口中删除所有矩形和按钮
for rectangle in rectangles:
rectangles[rectangle].place_forget()
for delete in delete_buttons:
delete_buttons[delete].destroy()
# 从字典中删除所有矩形和按钮
rectangles.clear()
delete_buttons.clear()
# 从数据列表中删除特定的数据
data_list.pop(arg)
# 重新创建所有矩形和按钮,但现在数据列表少了一个项目
CreateRectangles()
for num in delete_buttons:
delete_buttons[num].bind("<ButtonPress-1>", lambda event, arg=num: DeleteClick(event, arg))
window.mainloop()
它只在第一次运行时有效。例如,如果我删除一个项目,它不会做任何其他操作。
有什么问题?
英文:
I'm trying to show a list of elements from a data set in a tkinter window. I want to able to manipulate the elements, by highlighting, deleting etc.
I have this code:
from tkinter import *
window = Tk()
window.geometry("100x100")
#data from API
data_list = [
["1", "Lorem"],
["2", "Lorem"],
["3", "Lorem"],
["4", "Lorem"]
]
#create selectable rectangles from data_list with delete buttons
rectangles = {}
delete_buttons = {}
def CreateRectangles():
i = 0
for data in data_list:
rectangles[i] = Canvas(window, bg="#BFBFBF", height=15, width=80)
rectangles[i].place(x=19, y=20.0 + (i * 19))
rectangles[i].create_text(5.0, 1.0, anchor="nw", text=str(f'#{data[0]}:{data[1]}'))
delete_buttons[i] = Label(window, text="X ", bg="#D9D9D9")
delete_buttons[i].place(x=6, y=20.0 + (i * 19))
i += 1
CreateRectangles()
#highlight clicked rectangle
def RectangleClick(e, arg):
#reset how all rectangles look
for i in rectangles:
rectangles[i].config(bg="#BFBFBF")
#highlight the one clicked
rectangles[arg].config(bg="#999999")
for key in rectangles:
rectangles[key].bind("<ButtonPress-1>", lambda event, arg=key: RectangleClick(event, arg))
#delete button action
def DeleteClick(e, arg):
# delete all rectangles and buttons from window
for rectangle in rectangles:
rectangles[rectangle].place_forget()
for delete in delete_buttons:
delete_buttons[delete].destroy()
# delete all rectangles and buttons from dictionary
rectangles.clear()
delete_buttons.clear()
# delete the specific data from de data_list
data_list.pop(arg)
# re do everything but now the data list has one less item
CreateRectangles()
for num in delete_buttons:
delete_buttons[num].bind("<ButtonPress-1>", lambda event, arg=num: DeleteClick(event, arg))
window.mainloop()
It only works the first time. For example, if I delete an item, it doesn't do anything else.
What's wrong?
答案1
得分: 1
将绑定事件处理程序的所有代码移动到CreateRectangles
方法内部。由于所有先前的矩形都被销毁,事件处理程序需要重新附加。
英文:
Move all the code that binds event handlers inside the CreateRectangles
method. Since all the previous rectangles are destroyed, the event handlers need to be attached again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论