【问题标题】:How to delete frame on click of button? Python tkinter如何在单击按钮时删除框架? Python tkinter
【发布时间】:2023-01-30 13:16:56
【问题描述】:
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()

因此,我为一个按钮编写了一个函数,该函数在单击时创建一个框架,可以从上面的代码中读取。我还在添加的每个帧中添加了一个删除按钮,但我不知道如何让删除按钮起作用。

单击删除按钮时如何删除框架?有什么方法可以将 id 添加到 them= frames 吗?我必须为此使用 OOP 吗?

谢谢你。

【问题讨论】:

    标签: python tkinter button frames


    【解决方案1】:

    单击删除按钮时如何删除框架?

    你需要调用框架的destroy方法。

    newtaskdel_button = tk.Button(..., command = newtask_frame.destroy)
    

    【讨论】:

      猜你喜欢
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 2016-06-14
      相关资源
      最近更新 更多