亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

呼啦一陣風 2023-07-05 16:15:32
我在畫布中創建了一個框架,通過單擊鼠標在其中放置復選按鈕。選擇另一個單選按鈕,我運行命令 canvas.delete("all") 來清除畫布(以及帶有復選按鈕的框架),然后再次創建框架以繼續放置復選按鈕??蚣芤褎摻ǎ覠o法再放置檢查按鈕(我也沒有收到錯誤消息)。有人知道為什么嗎?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 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號