为了账号安全,请及时绑定邮箱和手机立即绑定

在 canvas.delete("all") 之后在画布中重用框架小部件不起作用

在 canvas.delete("all") 之后在画布中重用框架小部件不起作用

呼啦一阵风 2023-07-05 16:15:32
我在画布中创建了一个框架,通过单击鼠标在其中放置复选按钮。选择另一个单选按钮,我运行命令 canvas.delete("all") 来清除画布(以及带有复选按钮的框架),然后再次创建框架以继续放置复选按钮。框架已创建,但我无法再放置检查按钮(我也没有收到错误消息)。有人知道为什么吗?from tkinter import *root = Tk()top_canvas = Canvas(root,width=676,height=768, bg='light blue')top_canvas.pack()frame = Frame(top_canvas, bg='light grey')main_frame = top_canvas.create_window(500, 780, height = 1600, width = 600, window = frame)def place_checkbutton_in_canvas(e):  # order to insert the checkbutton    xx = e.x    yy = e.y    buttons = Checkbutton(frame)    buttons.place(x=xx, y=yy)def place_checkbutton():  #to run when checkbutton is selected. Now the checkbutton will be placed where mouse clicked if choose_line is selected    frame.bind('<Button-1>', place_checkbutton_in_canvas)def clear_canvas():    top_canvas.delete("all")    frame = Frame(top_canvas, bg='magenta')    main_frame = top_canvas.create_window(500, 780, height=1600, width=600, window=frame)chosen_option = IntVar()choose_checkbutton = Radiobutton(root, text = "place checkbutton", variable=chosen_option, value = 1, command = place_checkbutton)choose_checkbutton.place(x=10, y=10)clear_button = Radiobutton(root, text = "clear everything", variable=chosen_option, value = 2, command = clear_canvas)clear_button.place(x=10, y=100)root.mainloop()
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

无需删除画布上的所有项目。您可以使用列表来保存所有检查按钮,并使用.destroy()“删除”它们:


from tkinter import *


root = Tk()

top_canvas = Canvas(root,width=676,height=768, bg='light blue')

top_canvas.pack()

root.checkbutton_list = [] # initial a list


frame = Frame(top_canvas, bg='light grey')

main_frame = top_canvas.create_window(500, 780, height = 1600, width = 600, window = frame)


def place_checkbutton_in_canvas(e):  # order to insert the checkbutton

    xx = e.x

    yy = e.y

    buttons = Checkbutton(frame)

    buttons.place(x=xx, y=yy)

    root.checkbutton_list.append(buttons) # append the checkbutton to the list


def place_checkbutton():  #to run when checkbutton is selected. Now the checkbutton will be placed where mouse clicked if choose_line is selected

    frame.bind('<Button-1>', place_checkbutton_in_canvas)


def clear_canvas():

    for i in root.checkbutton_list:

        i.destroy() # destroy all the checkbuttons in the list

    root.checkbutton_list = [] # init it again


chosen_option = IntVar()

choose_checkbutton = Radiobutton(root, text = "place checkbutton", variable=chosen_option, value = 1, command = place_checkbutton)

choose_checkbutton.place(x=10, y=10)

clear_button = Radiobutton(root, text = "clear everything", variable=chosen_option, value = 2, command = clear_canvas)

clear_button.place(x=10, y=100)


root.mainloop()

如果你真的想将它与 一起使用.delete(ALL)。你需要更改 checkbutton 的父容器。我用它root.frame来覆盖前一帧。例如:


from tkinter import *


root = Tk()

top_canvas = Canvas(root,width=676,height=768, bg='light blue')

top_canvas.pack()


root.frame = Frame(top_canvas, bg='light grey')

main_frame = top_canvas.create_window(500, 780, height = 1600, width = 600, window = root.frame)


def place_checkbutton_in_canvas(e):  # order to insert the checkbutton

    xx = e.x

    yy = e.y

    buttons = Checkbutton(root.frame)

    buttons.place(x=xx, y=yy)


def place_checkbutton():  #to run when checkbutton is selected. Now the checkbutton will be placed where mouse clicked if choose_line is selected

    root.frame.bind('<Button-1>', place_checkbutton_in_canvas)


def clear_canvas():

    top_canvas.delete("all")

    root.frame = Frame(top_canvas, bg='magenta')

    main_frame = top_canvas.create_window(500, 780, height=1600, width=600, window=root.frame)


chosen_option = IntVar()

choose_checkbutton = Radiobutton(root, text = "place checkbutton", variable=chosen_option, value = 1, command = place_checkbutton)

choose_checkbutton.place(x=10, y=10)

clear_button = Radiobutton(root, text = "clear everything", variable=chosen_option, value = 2, command = clear_canvas)

clear_button.place(x=10, y=100)


root.mainloop()


查看完整回答
反对 回复 2023-07-05
  • 1 回答
  • 0 关注
  • 69 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信