Deleting a specific widget in tkinter

Viewed 62
#importing modules required
import tkinter as tk
import tkinter.messagebox
import tkcalendar as tkcal

#initializing global variables
x,y=50,100 
tables={}
dateoftable=''
dateslist=[]
statusbartext = 'Status: Press on the button to create a new table'
btnnumber=0

#creating the main window
root=tk.Tk()
root.title("Time Table")
root.geometry("800x600")

#Label that says the instruction
mainLabel=tk.Label(root,text="Select Timetable below")
mainLabel.place(x=10,y=10)

#button to create a new table
newtablebtn=tk.Button(root,text="+Create Table")
newtablebtn.place(x=320,y=50,width=90,height=30)

#button to delete a table
deletetablebtn=tk.Button(root,text="X Delete All Tables")
deletetablebtn.place(x=430,y=50,width=100,height=30)

#status bar at the bottom right
statusbar=tk.Label(root,text=statusbartext)
statusbar.place(x=500,y=570)

#function opens a new window to choos date and creates a button in main window for that date
def createtable():
    global dateoftable,dateslist,btnnumber
    
    #initializing the window containing the date picker
    calframe=tk.Tk()
    calframe.title("Date Picker")
    calframe.geometry('400x400')

    #Creating the calendar widget
    cal = tkcal.Calendar(calframe, year=2021, month=1, day=1, selectmode='day',
                         showothermonthdays=False, date_pattern='dd-mm-y')
    cal.place(x=70,y=20)

    #Creating the button to pick the date
    getdatebtn=tk.Button(calframe, text="Get Date")
    getdatebtn.place(x=160,y=260)

    #function that gets the date and creates a new table
    def grad_date():
        global y, x, tables, dateoftable, statusbartext, dateslist,btnnumber

        #Storing the date picked into the global variable
        dateoftable=cal.get_date() 

        #checking if date  picked already exists
        if dateoftable not in dateslist:
            
            dateslist.append(dateoftable)
            btnnumber+=1
            #Creating the button for that specific date
#THIS IS THE PART WHERE THE btn.number AND STUFF COMES IN
            btn = tk.Button(root, text=dateoftable)
            btn.number=btnnumber
            btn.place(x=x, y=y, width=100, height=100)

            #Appending the button object to a list
            tables[btnnumber]=btn

            #for the button to not go beyond the main window's border
            if y <= 400: 
                y += 120
            else:
                if x < 600:
                    x += 120
                    y = 100
                #if both boundaries limit's are reached 
                else:
                    statusbartext = 'Status: No more tables can be created!'
                    newtablebtn.configure(command=None)
                    statusbar.configure(text=statusbartext)

            #destroying the date picker window once the date is picked
            calframe.destroy()

        else:

            tkinter.messagebox.showwarning("Warning", "Table for the date picked already exists. Please choose another date.")
            calframe.destroy()
        

    getdatebtn.configure(command=grad_date)
    
    calframe.mainloop()

    
#function that deletes all the created tables
def deletetables():
    global tables,x,y,dateoftable,dateslist,statusbartext,btnnumber
    
    for table in tables.values():
        table.destroy()
    #resetting the global variables
    tables.clear()
    btnnumber=0
    dateoftable=''
    dateslist.clear()
    statusbartext = 'Status: Press on the button to create a new table'
    statusbar.configure(text=statusbartext)
    x,y=50,100


#I just kept this function as a placeholder action to occur when the button is clicked
def tableclick():
    tkinter.messagebox.showinfo("Warning", "Table for the date picked already exists. Please choose another date.")



#configuring buttons to perform actions
newtablebtn.configure(command=createtable)
deletetablebtn.configure(command=deletetables)

for button in tables.values():
    button.configure(command=tableclick)
 
root.mainloop()

So here in the deletetables() function I would like the delete button to call this when clicked.

I would be fine with anything, either deleting all the tables (buttons) at once, or deleting each table (button) one by one when clicked. I would also want to know how to access each table (button) that was created separately, because I want them to have their own attributes.

2 Answers

Create a global list to store all the tables and then delete them from the list.

tables = []
#...
def createtable():
    global y, x
    
    btn=tk.Button(root,text="Table")
    btn.place(x=x,y=y,width=100,height=100)
    tables.append(btn) #Append it to tables

    #for the button to not go beyond the main window's border
    if y <= 400:
        y += 120
    else:
        if x<600:
            x += 120
            y = 100
        else:
            newtablebtn.configure(command=nocommand)
            statusbar.configure(text="Status: No more tables can be created!")
#...
def deletetables():
    global tables, x, y
    for table in tables:
        table.destroy()
    tables.clear()
    x, y = 50, 100

Also notice a mistake in deletetablebtn.configure(command=deletetables)(No brackets).

Place the buttons in a new frame, and clear the frame using -

for widget in frame.winfo_children():
    widget.destroy()

Using globals is not recommended, but i still used them -

import tkinter as tk 

x,y=10,100 #global variables for positions

root=tk.Tk()
root.title("Time Table")
root.geometry("800x600")

frame1 = tk.Frame(root,width=750,height=600)
frame1.place(x=25,y=30)

mainLabel=tk.Label(root,text="Select Timetable below")
mainLabel.place(x=10,y=10)

#button to create a new table
newtablebtn=tk.Button(root,text="+Create Table")
newtablebtn.place(x=320,y=50,width=90,height=30)

#button to delete a table
deletetablebtn=tk.Button(root,text="X Delete All Tables")
deletetablebtn.place(x=430,y=50,width=100,height=30)

#status bar at the bottom right
statusbar=tk.Label(root,text='Status: ')
statusbar.place(x=550,y=570)

#Using this function for buttons that shouldn't perform any function;command=Null didn't work
def nocommand():
    pass

#function creates buttons, as a placeholder for tables
def createtable():
    global y
    global x
    
    btn=tk.Button(frame1,text="Table")
    btn.place(x=x,y=y,width=100,height=100)
    
    #for the button to not go beyond the main window's border
    if y <= 400:
        y += 120
    else:
        if x<600:
            x += 120
            y = 100
        else:
            newtablebtn.configure(command=nocommand)
            statusbar.configure(text="Status: No more tables can be created!")
    
#THE FUNCTION I NEED HELP WITH 
def deletetables():
    global x,y
    x,y=10,100
    for widgets in frame1.winfo_children():
        widgets.destroy()

#configuring buttons to perform actions
newtablebtn.configure(command=createtable)
deletetablebtn.configure(command=deletetables)

root.mainloop()
Related