#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按钮时调用它
我可以做任何事情,要么一次删除所有表(按钮),要么在单击时逐个删除每个表(按钮)。我还想知道如何访问单独创建的每个表(按钮),因为我希望它们有自己的属性
创建一个全局列表以存储所有表,然后将它们从列表中删除
还请注意
deletetablebtn.configure(command=deletetables)
(无括号)中的错误将按钮放置在新框架中,并使用以下方法清除框架:
不建议使用globals,但我仍然使用它们-
相关问题 更多 >
编程相关推荐