[英]How to delete frame on click of button? Python tkinter
import tkinter as tk
global counter
counter = 0
def addframe():
global counter
newtask_frame = tk.Frame(highlightbackground="green",highlightthickness=1, master = intrldisp_frame)
newtaskheading_label = tk.Label(newtask_frame, text="Added frame")
newtaskheading_label.grid(row=0,column=0)
newtaskdel_button = tk.Button(newtask_frame,text="del", command = newtask_frame.grid_forget())
newtaskdel_button.grid(row=1,column = 0,pady=5,padx=10)
newtask_frame.grid(row=counter,column=0,sticky = "nsew",pady=10,padx=5)
counter+=1
window = tk.Tk()
tasksinfo_frame = tk.Frame(highlightbackground="red",highlightthickness=2)
tasksinfo_frame.grid(row=0,pady=10)
addtask_button = tk.Button(tasksinfo_frame, text = "add",activebackground="blue",command = addframe)
addtask_button.pack(fill=tk.BOTH)
intrldisp_frame = tk.Frame(highlightbackground="blue",highlightthickness=1, master = window)
intrldisp_frame.grid(row=1,column=0,sticky = "n", padx=5)
window.mainloop()
因此,我为一个按钮编写了一个 function,该按钮在单击时创建一个框架,可以从上面的代码中读取。 我还在添加的每个帧中添加了一个删除按钮,但我不知道如何让删除按钮起作用。
单击删除按钮时如何删除框架? 有什么方法可以将 id 添加到 them= frames 吗? 我必须为此使用 OOP 吗?
谢谢。
单击删除按钮时如何删除框架?
您需要调用框架的destroy
方法。
newtaskdel_button = tk.Button(..., command = newtask_frame.destroy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.