如何删除嵌入在窗口中的按钮? (Tkinter)

时间:2019-04-26 08:22:58

标签: python-3.x button tkinter tkinter-canvas

我为游戏创建了一个播放按钮,当我们单击它时想将其删除。

但是,我将其与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)

2 个答案:

答案 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()

希望这会有所帮助。