#importing modules required。
import tkinter as tk
import tkinter.messagebox
import tkcalendar as tkcal
#initializing global variables[/span
x,y=50,100
tables={}
dateoftable=''/span>
dateslist=[]
statusbartext = '狀態。按下按鈕,創建一個新的表格'。
btnnumber=0
#創建主視窗。
root=tk.Tk()
root.title("Time Table")
root.geometry("800x600")
#Label that says the instruction[/span].
mainLabel=tk.Label(root,text="選擇以下時間表")
mainLabel.place(x=10, y=10)
#button to create a new table[/span]。
newtablebtn=tk.Button(root,text=" Create Table")
newtablebtn.place(x=320,y=50, width=90, height=30)
#button to delete a table[/span]。
deletetablebtn=tk.Button(root,text="X Delete All Tables"/span>)
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("日期選擇器")
calframe.geometry('400x400')
#Creating the calendar widget2021, 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[/span]。
getdatebtn=tk.Button(calframe, text="獲取日期")
getdatebtn.place(x=160, y=260)
#function that gets the date and create a new table[/span]。
def grad_date() 。
global y, x, tables, dateoftable, statusbartext, dateslist,btnnumber
#Storing the date picked into the global variable[/span]。
dateoftable=cal.get_date()
#檢查所選日期是否已經存在。
if dateoftable not in dateslist:
dateslist.append(dateoftable)
btnnumber =1
#創建該特定日期的按鈕。
#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)
#將按鈕物件添加到一個串列中。
tables[btnnumber]=btn
#for the button to not go beyond the main window's border。
if y <= 400:
y = 120: y = 120.
else:
if x < 600:
x = 120 100: x = 120.
#if both boundaries limit's are reached。
else:
statusbartext = '狀態。沒有更多的表可以被創建!'
newtablebtn.configure(command=None)
statusbar.configure(text=statusbartext)
#destroying the date picker window once the date is picked[/span].
calframe.destroy()
else:
tkinter.messagebox.showwarning("警告", "所選日期的表格已經存在。請選擇另一個日期。")
calframe.destroy()
getdatebtn.configure(command=grad_date)
calframe.mainloop()
#函式,洗掉所有創建的表。
def deletetables()。
global tables, x, y, dateoftable, dateslist, statusbartext, btnnumber
for table in tables.values()。
table.destroy()
#resetting the global variables: table.destroy()
tables.clear()
btnnumber=0
dateoftable=''。
dateslist.clear()
statusbartext = '狀態。按下按鈕,創建一個新的表格'。
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("警告", "所選日期的表格已經存在。請選擇其他日期。")
#配置按鈕來執行動作。
newtablebtn.configure(command=createtable)
deletetablebtn.configure(command=deletetables)
for按鈕in tables.values()。
button.configure(command=tableclick)
root.mainloop()
因此,在deletetables()函式中,我希望洗掉按鈕在被點擊時呼叫這個。
我對任何事情都沒有意見,要么一次洗掉所有的表(按鈕),要么在點擊時逐一洗掉每個表(按鈕)。我還想知道如何訪問單獨創建的每個表(按鈕),因為我希望它們有各自的屬性。
uj5u.com熱心網友回復:
創建一個全域串列來存盤所有的表,然后將它們從串列中洗掉。
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) #將其添加到tables中
#for the button to not go beyond the main window's border[/span]。
if y <= 400:
y = 120: y = 120.
else:
if x<600:
x = 120 100: x = 120.
else:
newtablebtn.configure(command=nocommand)
statusbar.configure(text="狀態。沒有更多的表可以被創建!")
#...
def deletetables()。
global tables, x, y
for table in tables:
table.destroy()
tables.clear()
x, y = 50, 100
同時注意到deletetablebtn.configure(command=deletetables)中的一個錯誤(沒有括號)。
uj5u.com熱心網友回復:
將按鈕放在一個新的框架中,并使用-
清除框架。for widget in frame.winfo_children() 。
widget.destroy()
不推薦使用globals,但我還是使用了它們 -
import tkinter as tk
x,y=10,100 #全域變數的位置。
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[/span]。
newtablebtn=tk.Button(root,text=" Create Table")
newtablebtn.place(x=320,y=50, width=90, height=30)
#button to delete a table[/span]。
deletetablebtn=tk.Button(root,text="X Delete All Tables"/span>)
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)
#對不應該執行任何功能的按鈕使用這個函式;command=Null不作業。
def nocommand() 。
pass
#function創建按鈕,作為表格的占位符。
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: y = 120.
else:
if x<600:
x = 120 100: x = 120.
else:
newtablebtn.configure(command=nocommand)
statusbar.configure(text="狀態。沒有更多的表可以被創建!")
#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[/span]。
newtablebtn.configure(command=createtable)
deletetablebtn.configure(command=deletetables)
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315139.html
標籤:
