Python字典、列表和for循环错误

huangapple go评论62阅读模式
英文:

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(&quot;100x100&quot;)
#data from API
data_list = [
[&quot;1&quot;, &quot;Lorem&quot;],
[&quot;2&quot;, &quot;Lorem&quot;],
[&quot;3&quot;, &quot;Lorem&quot;],
[&quot;4&quot;, &quot;Lorem&quot;]
]
#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=&quot;#BFBFBF&quot;, height=15, width=80)
rectangles[i].place(x=19, y=20.0 + (i * 19))
rectangles[i].create_text(5.0, 1.0, anchor=&quot;nw&quot;, text=str(f&#39;#{data[0]}:{data[1]}&#39;))
delete_buttons[i] = Label(window, text=&quot;X &quot;, bg=&quot;#D9D9D9&quot;)
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=&quot;#BFBFBF&quot;)
#highlight the one clicked
rectangles[arg].config(bg=&quot;#999999&quot;)
for key in rectangles:
rectangles[key].bind(&quot;&lt;ButtonPress-1&gt;&quot;, 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(&quot;&lt;ButtonPress-1&gt;&quot;, 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.

huangapple
  • 本文由 发表于 2023年2月8日 23:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定