eachmonth = [0,31,28,31,30,31,30,31,31,30,31,30,31]
week = ['下标为0, 占个位', '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
from tkinter import *
root = Tk()
def is366(n):
if (n % 4 == 0 and n % 100) or n % 400 == 0:
return True
else:
return False
def whatday(year, month, day):
num366 = 0
if year >= 2022:#改成相对年份
if year >= 2024:#改成相对年份的下一个闰年
for each in range(2024#改成相对年份的下一个闰年, year+1, 4):
if is366(each):
num366 += 1
if is366(year) and month < 3:
num366 -= 1
deltday = (year - 2022#改成相对年份)*365 + sum(eachmonth[:month]) + (day -1) + num366
return (deltday-1) % 7
else:
if year <= 2020:#改成相对年份的上一个闰年
for each in range(year, 2022#改成相对年份, 4):
if is366(each):
num366 += 1
if is366(year) and month >= 3:
num366 -= 1
deltday = (year - 2022#改成相对年份)*365 + sum(eachmonth[:month]) + (day -1) - num366
return (deltday-1) % 7
class Date:
def __init__(self, y, m, d):
self.y = y
self.m = m
self.d = d
self.w = whatday(self.y, self.m, self.d)
self.row = (self.d + bnum)//7 +2 if (self.d + bnum)%7 else (self.d + bnum)//7 + 1
data = []
y = int(input('请输入年:'))
m = int(input('请输入月:'))
em = eachmonth[m]
bnum = whatday(y, m, 1)
if m == 2:
if is366(y):
em = 29
for i in range(1, em+1):
data.append(Date(y, m, i))
Label(root, text=f'{y}年', fg='blue').grid(row=0, column=2)
Label(root, text=f'{m}月', fg='blue').grid(row=0, column=4)
i = -1
j = 0
while i < 6:
while j < 7:
i += 1
j += 1
Label(root, text=week[j], fg='red').grid(row=1, column=i)
for each in data:
if each.w == 6 or each.w == 0:
Label(root, text=str(each.d), fg='red').grid(row=each.row, column=each.w)
else:
Label(root, text=str(each.d)).grid(row=each.row, column=each.w)
mainloop()
42行普通敢对我61行tkinter吗?