Python日历控件
时间: 2024-04-06 11:26:59 浏览: 7
以下是两种Python日历控件的例子:
1. Python tkinter下拉日历控件代码[^1]:
```python
import tkinter as tk
from tkinter import ttk
from tkcalendar import Calendar
def get_date():
selected_date = cal.get_date()
print("Selected Date:", selected_date)
root = tk.Tk()
cal = Calendar(root, selectmode="day")
cal.pack()
button = ttk.Button(root, text="Get Date", command=get_date)
button.pack()
root.mainloop()
```
2. Python自定义日历控件开发[^2]:
```python
import tkinter as tk
from tkinter import ttk
from datetime import datetime
class CalendarWidget(tk.Frame):
def __init__(self, parent):
super().__init__(parent)
self.calendar = ttk.Treeview(self)
self.calendar.pack()
self.populate_calendar()
def populate_calendar(self):
# 获取当前日期
today = datetime.today().date()
# 添加日期到日历控件
for i in range(1, 32):
date = datetime(today.year, today.month, i).date()
self.calendar.insert("", "end", text=str(date))
root = tk.Tk()
calendar_widget = CalendarWidget(root)
calendar_widget.pack()
root.mainloop()
```