使用Python实现波斯日历规则的困难

时间:2018-05-10 09:20:59

标签: python tkinter

我正在编写一个类似于波斯日历的代码。年,月,日有3个下拉列表。以下是我想要包含的规则:

  • 第1至第6个月有31天
  • 第7个月到第11个月有30天
  • 第12个月每4年有29天
  • 第12个月有30天(闰年)

如果用户选择(1 - 2 - 3 - 4 - 5 - 6)个月中的一个,那么天数的下拉列表必须有31天。

如果用户选择其中一个(7 - 8 - 9 - 10 - 11)个月,则下拉列表中的天数必须为30天。

如果用户选择了第12个月,那么天数的下拉列表必须有29天。

如果用户选择(1375 - 1379 - 1383 - 1387 - 1391 - 1395)年中的一个,如果他选择了第12个月,那么天数的下拉列表必须有30天。

这是我到目前为止编写的代码,但我的代码不起作用,请帮助我。

from tkinter import *

x=StringVar()

def ok():
    if months == months[0:5]:
     x = dayoptions1
    if months == months[6:10]:
     x = dayoptions2
    if months == months[11] and years == 1375 or 1379 or 1383 or 1387 or 1391 or 1395:
     x = dayoptions3


root = Tk()

label1 = Label(root, text="year",width=15)
label1.grid(row=0, column=0)

yearoptions = ["1397", "1396","1395","1394","1393","1392","1391","1390","1389","1388","1387","1386","1385","1384","1383","1382","1381","1380","1379","1378","1377","1376","1375"]
yearvariable = StringVar(root)
yearvariable.set(yearoptions[0])

years = OptionMenu(root, yearvariable, *yearoptions)
years.grid(row=0,column=1,padx=5, pady=5)

label2 = Label(root, text="month",width=15)
label2.grid(row=0, column=2)

monthoptions = ["1", "2","3","4","5","6","7","8","9","10","11","12"]
monthvariable = StringVar(root)
monthvariable.set(monthoptions[0])

months = OptionMenu(root, monthvariable, *monthoptions)
months.grid(row=0,column=3,padx=5, pady=5)

label1 = Label(root, text="day",width=15)
label1.grid(row=0, column=4)

dayoptions1 = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]
dayoptions2 = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"]
dayoptions3 = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29"]
dayvariable = StringVar(root)
dayvariable.set("1")

days = OptionMenu(root, dayvariable, *x)
days.grid(row=0,column=5,padx=5, pady=5)


root.mainloop()

1 个答案:

答案 0 :(得分:0)

好吧,我不是tkinter,所以我写了一个函数,但它有效。天数取决于月份和时间。年用户选择。所以情况是,用户选择1383 - 12(第12个月的第1383年),您需要查找1383是否为闰年,然后填写30天的日期列表,否则它将是29天。如果使用在第1个月到第6个月之间选择,则在31天内填充阵列,其余时间为30天。我已经写了一个简短的帮助函数来检查这个,我认为它有效。

def persian_calender():
    user_input = input("year - month:: ")
    date_list = user_input.split("-")
    leap_year = [1375, 1379 ,1383, 1387 ,1391 ,1395]
    year = int(date_list[0])
    month = int (date_list[1])

    if year in leap_year:
        is_leap = True
    else:
        is_leap = False

    if month >= 1 and month <= 6:
        number_of_days = "31"
    elif month >= 7 and month <= 11:
        number_of_days = "30"
    else:
        if is_leap:
            number_of_days = "30"
        else:
            number_of_days = "29"
     print ("Year: " + date_list[0] + " Month: "+ date_list[1] + " Days: "
       + number_of_days)

检查code file in here

希望它有所帮助。干杯!

[编辑2]
你写了一个帮助函数Ok,找出它是什么月份。而是手动编写列表,只需声明其中的天数即可。如果您希望或有时间填写下拉列表,请运行并填写相同天数的列表。 我有意义吗?!