我尝试建立一个日历,显示一年中的当前月份,并按下<<和>>按钮,可以看到上个月或下个月。 代码似乎一见钟情,但是当你想要查看前一个/下个月时,框架并没有完全清除,并显示前几个月的剩余天数,例如31个月只有30天。 我无法弄清楚如何移除它们。你能帮我吗?感谢
from tkinter import Tk, RAISED, Label, Button, Frame
class org(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
def organizer(self, xx, yy):
from calendar import monthrange, month
monthname = str()
m = month(yy, xx)
for i in m:
if i == '\n':
break
monthname +=i
previous = Button(self, text = '<<', command = self.prev)
previous.grid(row = 0, column = 0)
nextt = Button(self, text = '>>', command = self.ntt)
nextt.grid(row = 0, column = 6)
month = Label(self, text = monthname+ ' ')
month.grid(row = 0, column = 1, columnspan = 5)
labels = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']
for i in range(len(labels)):
days = Label(self, text = labels[i], width = 3)
days.grid(row = 1, column = i)
(startday, endday) = monthrange(yy, xx)
r = 2
for i in range(0,startday):
label = Label(self, width=3, text=' ')
label.grid(row = 2, column = i)
for i in range(1, endday+1):
label = Button(self, text = i, width = 3)
label.grid(row = r, column = startday)
startday +=1
if startday > 6:
startday -= 7
r += 1
for i in range(startday, 7):
label = Label(self, width=3, text=' ')
label.grid(row = r, column = i)
class Cal(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack()
self.x = 10 #Oct
self.y =2016 #2016
org.organizer(self,self.x, self.y)
def prev(self):
Frame.grid_remove(self)
self.x -= 1
if self.x==0:
self.x = 1
else:
org.organizer(self,self.x, self.y)
def ntt(self):
self.x += 1
if self.x==13:
self.x = 12
else:
org.organizer(self,self.x, self.y)
root = Tk()
a = Cal(root)
a.pack()
root.mainloop()
答案 0 :(得分:0)
好像你想删除在Tk()上绘制的所有小部件并重绘它。您可以使用以下代码删除所有小部件:
for child in root.winfo_children():
child.destroy()