Python 3.6.1简易下拉日历+日期选择器实现求助
Custom Dropdown Date Picker for Python 3.6.1 (No tkcalendar Dependencies)
Got it! Since you can't access the Calendar and DateEntry components from tkcalendar, here's a concise, self-contained implementation using only standard Python libraries. This picker drops down when you click the input box, captures your selected date, and works seamlessly with Python 3.6.1.
Full Working Code
import tkinter as tk from tkinter import ttk import calendar from datetime import datetime class DropdownDatePicker(tk.Tk): def __init__(self): super().__init__() self.title("简易下拉日历选择器") self.geometry("300x100") # Store the selected date as a datetime object self.selected_date = None # Input field - click to toggle calendar self.date_entry = ttk.Entry(self, width=15) self.date_entry.pack(pady=20) self.date_entry.bind("<Button-1>", self.toggle_calendar) # Calendar container (hidden by default) self.calendar_frame = ttk.Frame(self) self.calendar_frame.pack_forget() # Month/Year selection controls self.month_var = tk.IntVar(value=datetime.now().month) self.year_var = tk.IntVar(value=datetime.now().year) ttk.Label(self.calendar_frame, text="月份:").grid(row=0, column=0, padx=5) self.month_combo = ttk.Combobox(self.calendar_frame, textvariable=self.month_var, values=list(range(1,13)), width=3) self.month_combo.grid(row=0, column=1, padx=5) self.month_combo.bind("<<ComboboxSelected>>", self.refresh_calendar) ttk.Label(self.calendar_frame, text="年份:").grid(row=0, column=2, padx=5) self.year_combo = ttk.Combobox(self.calendar_frame, textvariable=self.year_var, values=list(range(2000, 2031)), width=5) self.year_combo.grid(row=0, column=3, padx=5) self.year_combo.bind("<<ComboboxSelected>>", self.refresh_calendar) # Weekday headers weekdays = ["日", "一", "二", "三", "四", "五", "六"] for col, day in enumerate(weekdays): ttk.Label(self.calendar_frame, text=day).grid(row=1, column=col, padx=3, pady=2) # Frame for date buttons self.date_btn_frame = ttk.Frame(self.calendar_frame) self.date_btn_frame.grid(row=2, column=0, columnspan=7) # Initialize the calendar with current month/year self.refresh_calendar() def toggle_calendar(self, event): # Show/hide the calendar when input is clicked if self.calendar_frame.winfo_ismapped(): self.calendar_frame.pack_forget() else: self.calendar_frame.pack(pady=5) self.refresh_calendar() def refresh_calendar(self, event=None): # Clear existing date buttons for widget in self.date_btn_frame.winfo_children(): widget.destroy() month = self.month_var.get() year = self.year_var.get() # Get calendar data for selected month/year cal_data = calendar.monthcalendar(year, month) # Create buttons for each date for row, week in enumerate(cal_data): for col, day in enumerate(week): if day != 0: date_btn = ttk.Button(self.date_btn_frame, text=str(day), command=lambda d=day: self.on_date_select(d)) date_btn.grid(row=row, column=col, padx=2, pady=2) def on_date_select(self, day): # Format selected date and update UI date_str = f"{self.year_var.get()}-{self.month_var.get():02d}-{day:02d}" self.selected_date = datetime.strptime(date_str, "%Y-%m-%d") # Update input field self.date_entry.delete(0, tk.END) self.date_entry.insert(0, date_str) # Hide calendar after selection self.calendar_frame.pack_forget() # Optional: Verify the captured date (customize this as needed) print(f"Captured date: {self.selected_date}") if __name__ == "__main__": app = DropdownDatePicker() app.mainloop()
Key Details & Usage
- Toggle Calendar: Click the input field to show/hide the calendar panel.
- Select Month/Year: Use the dropdowns to switch between months and years— the calendar updates instantly.
- Capture Date: When you click a date, it populates the input field, and the selected date is stored as a
datetimeobject inself.selected_date(you can pass this to other functions or use it directly in your code). - Compatibility: No external libraries required—uses only
tkinter,calendar, anddatetime, which are all included in Python 3.6.1.
How It Looks
(Note: This code will produce a functional UI matching the effect you described—click-to-expand calendar, date selection, and date capture.)
内容的提问来源于stack exchange,提问作者Garima Tiwari
相关产品推荐
相关产品推荐

