python tkinter 日历控件

关于日期选择的控件 (简陋版)

import tkinter as tk
import tkinter.ttk as ttk
import datetime

import calendar

today_date=datetime.date.today()
today_date_year = int(today_date.year)
today_date_month = int(today_date.month)
today_date_week = today_date.isocalendar()

date_num_list = calendar.monthcalendar(today_date_year,today_date_month)

my_win=tk.Tk()
my_win.title("日历")

def calendar_date():
    win_calendar = tk.Toplevel(my_win)
    win_calendar.title("日历")
    win_calendar.geometry('300x250')

    calendar_Canvas = tk.Canvas(win_calendar,highlightthickness=0,width=300,height=250)
    calendar_Canvas.pack()
    year_list = tuple(range(today_date_year-10,today_date_year+1))
    month_list = tuple(range(1,13))

    def Button_year_jian():
        calendar_Canvas.delete(tk.ALL)
        year_txt1 = year_txt.get()
        year_txt2 = year_txt1-1
        year_txt.set(year_txt2)
        my_calendar()

    def Button_year_jia():
        calendar_Canvas.delete(tk.ALL)
        year_txt1 = year_txt.get()
        year_txt2 = year_txt1+1
        year_txt.set(year_txt2)
        my_calendar()

    def Button_month_jian():
        calendar_Canvas.delete(tk.ALL)
        month_txt1 = month_txt.get()
        month_txt2 = 12 if month_txt1==1 else month_txt1-1
        month_txt.set(month_txt2)
        my_calendar()

    def Button_month_jia():
        calendar_Canvas.delete(tk.ALL)
        month_txt1 = month_txt.get()
        month_txt2 = 1 if month_txt1==12 else month_txt1+1
        month_txt.set(month_txt2)
        my_calendar()
        
    def Button_date(event):
        year_txt1 = year_txt.get()
        month_txt1 = month_txt.get()
        Button_txt = int(event.widget['text'])
        
        new_date = datetime.date(year_txt1,month_txt1,Button_txt)
        date.set(new_date)

    def year_month_date(event):
        my_calendar()

        

    ttk.Button(calendar_Canvas,width=2,text="◀",command= Button_year_jian).place(x=5,y=5)
    year_txt = tk.IntVar()
    year_txt.set(today_date_year)
    year_date = ttk.Combobox(calendar_Canvas,width=6,values=year_list,textvariable=year_txt)
    year_date.place(x=30,y=7)
    year_date.bind("",year_month_date)
    ttk.Button(calendar_Canvas,width=2,text="▶",command= Button_year_jia).place(x=95,y=5)

    ttk.Button(calendar_Canvas,width=2,text="◀",command= Button_month_jian).place(x=185,y=5)
    month_txt = tk.IntVar()
    month_txt.set(today_date_month)
    month_date = ttk.Combobox(calendar_Canvas,width=6,values=month_list,textvariable=month_txt)
    month_date.place(x=210,y=7)
    month_date.bind("",year_month_date)
    ttk.Button(calendar_Canvas,width=2,text="▶",command= Button_month_jia).place(x=275,y=5)



    def my_calendar():
        calendar_Canvas.delete(all)
        
        date_num_list = calendar.monthcalendar(year_txt.get(),month_txt.get())
        date_num_y = 80
        
        calendar_Canvas.create_rectangle(15,40,285,60,fill="#d6d6db",width=0)
        week_list = ["一","二","三","四","五","六","日"]
        week_x = 30
        for i in week_list:
            calendar_Canvas.create_text(week_x,50,text=i)
            week_x = week_x+40
            
        for i in date_num_list:
            date_num_x = 30
            for ii in i:
                if ii == 0:
                    pass
                else:
                    My_date_Button = ttk.Button(calendar_Canvas,text=ii,width=2)
                    calendar_Canvas.create_window(date_num_x,date_num_y,window=My_date_Button)
                    My_date_Button.bind("", Button_date)
                date_num_x = date_num_x+40
            date_num_y = date_num_y+30

    my_calendar()


date = tk.StringVar()
date.set(today_date)
ttk.Entry(my_win,textvariable=date).pack(side="left",padx=5,pady=5)

ttk.Button(my_win,text="▼",width=2,command=calendar_date).pack(side="left",padx=5,pady=5)


my_win.mainloop()

在通过选择输入年月或下拉选择时,通过[Enter]键即可刷新日历界面,点击日期即可获取想要的日期

你可能感兴趣的:(tkinter,python,开发语言,pycharm)