我为游戏创建了一个播放按钮,当我们单击它时想将其删除。
但是,我将其与canvas.create_window放在窗口中,因此无法使用“ pack_forget”将其删除。
您知道如何删除它吗?
bouton_play=PhotoImage(file="bouton_play.gif")
Jouer=Button(fenetre,image=bouton_play,relief=FLAT,borderwidth=0,highlightthickness=0,command=debut)
canvas.create_window(144,450,window=Jouer)
答案 0 :(得分:2)
将按钮插入画布后,必须将其视为画布元素,并使用canvas.delete(tag_or_id)
代替widget.geometry_manager_forget()
import tkinter as tk
def debut():
print('debut')
canvas.delete(btnw) # <-- this removes the window containing the button
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack()
btn = Button(root, text='jouer', command=debut)
btnw = canvas.create_window(200, 200, window=btn) # <- this is the canvas element to delete from the canvas
root.mainloop()
答案 1 :(得分:-2)
尝试以下代码。单击该按钮后,它应该会消失。
import tkinter as Tk
def disappear():
but.grid_forget()
root = Tk.Tk()
canvas = Tk.Canvas(root, bg = 'black', width = 500, height = 500)
canvas.grid(row = 0, column = 0)
frame = Tk.Frame(bg = 'red')
canvas.create_window(0, 0, window = frame, anchor = Tk.NW)
but = Tk.Button(frame, text = 'Disappear', command = disappear)
but.grid(row = 0, column = 0)
root.mainloop()
希望这会有所帮助。