0

I am creating a tkinter program which takes in the policy number and date of birth of a customer. I wanted to create dropbox menus each for day, month, and year for obtaining the date of birth. So here's what I did:

years=[1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000]
months=['January','February','March','April','May','June','July','August','September','October','November','December']
days=[01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]

clicked1=IntVar()
clicked2=StringVar()
clicked3=IntVar()

dof_year=OptionMenu(root,clicked1,*years)
dof_month=OptionMenu(root,clicked2,*months)
dof_day=OptionMenu(root,clicked3,*days)

But then I realized a serious problem. Months have 30 and 31 days alternatively and February has 28 or 29 days.

I could prompt the user to first enter the year and month and click a button, then by using if-else statements, determine how many days I am supposed to give in the dropbox menu, and then show the days menu.

But that will look really unprofessional. So is there a way that when the user inputs the year and month, the "days" list updates dynamically without pressing any button?

Like for example, when I choose the year 1980 and the month "March", the 'days' menu instantly updates itself, so that when i click it, it shows me the exact number of days in that month.

Thanks!

  • what about tkcalendar? https://stackoverflow.com/questions/4443786/how-do-i-create-a-date-picker-in-tkinter – MVB76 Sep 25 '20 at 05:54
  • @MVB76 That would be nice too. But what about this method? –  Sep 25 '20 at 06:05
  • See [how-can-i-dynamic-populate-an-option-widget](https://stackoverflow.com/questions/7393430/how-can-i-dynamic-populate-an-option-widget-in-tkinter-depending-on-a-choice-fro/7403530#7403530) – acw1668 Sep 25 '20 at 06:23

1 Answers1

0

You can check here: Changing the options of a OptionMenu when clicking a Button on how to update the list of options in an OptionMenu. It is pretty cumbersome as OptionMenu has limited setters for updating its properties.

First choice would be to use a dedicated calendar widget.

Overwise, it would be much easier if you use a ComboBox which is quite similar visually and can be easily updated. Here is some sample code:

from tkinter import ttk
import tkinter as tk

root = tk.Tk()
years=[1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000]
months=['January','February','March','April','May','June','July','August','September','October','November','December']

def on_month():
    if month.get() in ('January','to be completed'):
        days = list(range(1,30))
    else:
        days = list(range(1,31))
    # todo: handle 28/29 days for Feb depending on year
    m.config(values=days)


year=tk.IntVar()
month=tk.StringVar()
day=tk.IntVar()

y = ttk.Combobox(root,values=years,state="readonly")
y.pack()

m = ttk.Combobox(root,values=months,state="readonly")
m.bind("<<ComboboxSelected>>", on_month)
m.pack()

d = ttk.Combobox(root,values=list(range(1,31)),state="readonly")
d.pack()
root.mainloop()
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20