You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现波斯日历规则遇阻,请求修复Tkinter下拉框代码

Fixing Your Persian Calendar Tkinter Dropdowns

Let's walk through fixing your code step by step—there are a few key issues with how you're accessing selected values, handling conditional logic, and updating the day dropdown dynamically.

Here's the corrected working code:

from tkinter import *

def update_days(*args):
    # Get selected year and month (convert to integers for easier comparison)
    selected_year = int(yearvariable.get())
    selected_month = int(monthvariable.get())
    
    # Clear existing day options
    days['menu'].delete(0, 'end')
    
    # Set day options based on your Persian calendar rules
    if 1 <= selected_month <= 6:
        day_options = [str(i) for i in range(1, 32)]
    elif 7 <= selected_month <= 11:
        day_options = [str(i) for i in range(1, 31)]
    else:  # Month is 12
        leap_years = {1375, 1379, 1383, 1387, 1391, 1395}
        if selected_year in leap_years:
            day_options = [str(i) for i in range(1, 31)]
        else:
            day_options = [str(i) for i in range(1, 30)]
    
    # Add new day options to the dropdown
    for day in day_options:
        days['menu'].add_command(label=day, command=lambda value=day: dayvariable.set(value))
    # Set default day to 1 after updating
    dayvariable.set(day_options[0])

root = Tk()
root.title("Persian Calendar")

# Year Dropdown Setup
label_year = Label(root, text="Year", width=15)
label_year.grid(row=0, column=0)
yearoptions = [str(year) for year in range(1375, 1398)]
yearvariable = StringVar(root)
yearvariable.set(yearoptions[-1])  # Default to 1397
years = OptionMenu(root, yearvariable, *yearoptions, command=update_days)
years.grid(row=0, column=1, padx=5, pady=5)

# Month Dropdown Setup
label_month = Label(root, text="Month", width=15)
label_month.grid(row=0, column=2)
monthoptions = [str(i) for i in range(1, 13)]
monthvariable = StringVar(root)
monthvariable.set(monthoptions[0])  # Default to month 1
months = OptionMenu(root, monthvariable, *monthoptions, command=update_days)
months.grid(row=0, column=3, padx=5, pady=5)

# Day Dropdown Setup
label_day = Label(root, text="Day", width=15)
label_day.grid(row=0, column=4)
dayvariable = StringVar(root)
days = OptionMenu(root, dayvariable, "")
days.grid(row=0, column=5, padx=5, pady=5)

# Initialize days with default year/month values
update_days()

root.mainloop()

Key fixes and improvements:

  • Fixed widget vs value confusion: You were trying to compare the OptionMenu widget itself (months, years) instead of the selected values. Now we use yearvariable.get() and monthvariable.get() to fetch the actual selected year/month, then convert them to integers for proper range checks.
  • Corrected conditional logic:
    • Replaced invalid list comparisons (like months == months[0:5]) with clear range checks for months.
    • Fixed the leap year check: The original years == 1375 or 1379... is invalid syntax—we now use a set of leap years and check if the selected year is in that set for clean, readable logic.
  • Added dynamic updates: We created an update_days function that triggers whenever the year or month dropdown changes (via the command parameter in OptionMenu). This function clears old day options and adds new ones based on the current selection.
  • Cleaned up option creation: Used list comprehensions to generate day options instead of hardcoding long lists—this makes the code shorter and easier to maintain if you ever need to adjust rules.
  • Proper initialization: We call update_days() once at startup to populate the day dropdown with the default year/month values, so it doesn't start empty.

内容的提问来源于stack exchange,提问作者user9769687

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:58:24