删除tkinter中的特定小部件

2024-09-30 12:31:19 发布

您现在位置:Python中文网/ 问答频道 /正文

#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()

因此,在deletetables()函数中,我希望单击delete按钮时调用它

我可以做任何事情,要么一次删除所有表(按钮),要么在单击时逐个删除每个表(按钮)。我还想知道如何访问单独创建的每个表(按钮),因为我希望它们有自己的属性


Tags: thetotexttablesdatetableplacebutton
2条回答

创建一个全局列表以存储所有表,然后将它们从列表中删除

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

还请注意deletetablebtn.configure(command=deletetables)(无括号)中的错误

将按钮放置在新框架中,并使用以下方法清除框架:

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

不建议使用globals,但我仍然使用它们-

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

相关问题 更多 >

    热门问题