简体   繁体   English

tkinter grid_forget 方法

[英]tkinter grid_forget method

The program I am creating updates the page with new data once the submit button is clicked, in the function for the submit button, it creates the labels (8 of them).单击提交按钮后,我正在创建的程序会使用新数据更新页面,在提交按钮的 function 中,它会创建标签(其中 8 个)。

row = 7
step = 0

for k,v in ordnance_database.items():
    name = v
    label = Label(window, text=(name + ": " + str(ship_ordnance[step])),
                  background=background_colour, foreground="white", font="none 10")
    label.grid(row=row, column=0, sticky=W, columnspan=2)

    all_labels.append(label)
    row += 1
    step += 1

When the submit button is clicked again, it creates new labels but the previous ones are still there.再次单击提交按钮时,它会创建新标签,但之前的标签仍然存在。 After a bit of googling and searching in the reference I found this: How to remove widgets from grid in tkinter?在参考资料中进行了一些谷歌搜索和搜索后,我发现了这一点: 如何从 tkinter 中的网格中删除小部件? and added this in the submit button's function:并将其添加到提交按钮的 function 中:

for labels in window.grid_slaves():
    if 7 < int(labels.grid_info()["row"]) < 15 and int(labels.grid_info()["column"]) == 0:
        labels.grid_forget()
    else:
        pass

I would like to clear the contents of the grids with row values 7 to 14, and in column 0.我想清除行值为 7 到 14 和第 0 列的网格的内容。

I have tried adding window.update() but this made no difference.我试过添加 window.update() 但这没有什么区别。

I have confirmed that to labels.grid_forget() is being triggered using print() but it's not actually clearing them from the screen.我已经确认 to labels.grid_forget()是使用print()触发的,但实际上并没有从屏幕上清除它们。

I have also tried using .configure - something like this我也尝试过使用.configure - 像这样

labels.configure(text="Loading")

but no joy.但没有快乐。

Here are screenshots from the program showing the issue.这是显示该问题的程序的屏幕截图。 On the second button click you can see "Homing: 8" has the previous data hidden behind it.点击第二个按钮,您可以看到“Homing: 8”隐藏了之前的数据。

第一个按钮点击

第二个按钮点击

The grid_forget() method will just hide the label, it won't remove the label. grid_forget()方法只会隐藏 label,它不会删除 label。

If you want to remove the label, use destroy() .如果要删除 label,请使用destroy() Now, if the objective here is just to update the label text, you can use StringVar()现在,如果这里的目标只是更新 label 文本,您可以使用StringVar()

row = 7
step = 0
for k,v in ordnance_database.items():
    name = v
    lbl_text_var = StringVar()
    lbl_text_var.set(name + ": " + str(ship_ordnance[step]))
    label = Label(window, textvariable=lbl_text_var, background=background_colour, foreground="white", font="none 10")
    # Setting text variable as it's own attribute to retrieve it later.
    # a separate list can also be created
    label.lbl_text_var = lbl_text_var
    all_labels.append(label)
    row += 1
    step += 1

now when you want to set the values现在当你想设置值时

step = 0
for k,v in ordnance_database.items():
    name = v
    lbl_text_var = all_labels[step].lbl_text_var
    lbl_text_var.set(name + ": " + str(ship_ordnance[step]))
    step += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM