hveagle 发表于 2022-8-5 09:24:52

1 月历

本帖最后由 hveagle 于 2022-9-18 14:09 编辑

eachmonth =
week = ['下标为0, 占个位', '星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']

from tkinter import *
root = Tk()



def is366(n):
    if (n % 4 == 0 and n % 100) orn % 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
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)
Label(root, text=week, fg='red').grid(row=1, column=0)
Label(root, text=week).grid(row=1, column=1)
Label(root, text=week).grid(row=1, column=2)
Label(root, text=week).grid(row=1, column=3)
Label(root, text=week).grid(row=1, column=4)
Label(root, text=week).grid(row=1, column=5)
Label(root, text=week, fg='red').grid(row=1, column=6)

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

hveagle 发表于 2022-9-22 17:29:48

啊这
页: [1]
查看完整版本: 1 月历