I am creating a calendar app using tkinter module. My problem is that I would like the user to select a specific date. That means I have 3 option menus (year, month, day) in the SAME frame. The problem is that the number of days in a month is not constat. So, after selecting a month, I would like the list of options in days to be updated (the list of options in days consists of integers ranging from 1 to the lenght of the sellected month). Is there a way to do it without defining a button with command that updates it, or without creating a new window? I am also using the Time module to calculate the current time and the Calendar module to calculate days in the chosen month. Thank you for your answers. Here is the chank of code:
from tkinter import *
import time
import calendar
root = Tk()
root.title("Calendar App")
root.geometry("960x540")
# Defining widgets in root
escape_button = Button(master=root, text="x")
escape_button.grid(row=0, column=0)
title = Label(master=root, text="TEST")
title.grid(row=0, column=1)
frame = LabelFrame(master=root, padx=50, pady=50)
frame.grid(row=1, column=1)
# Calculation of time
today = time.localtime()
y = time.strftime("%Y", today)
# ------- Year, Month, Day -------
# List of options for year
years_list = []
for i in range(12):
years_list.append(today.tm_year + i)
# Label
label_1 = Label(master=frame, text="Year:")
label_1.grid(row=0, column=0)
# Defining variable and option menu
year_var = IntVar()
year_var.set(today.tm_year)
year = OptionMenu(frame, year_var, *years_list)
year.grid(row=0, column=1)
# List of options for month
months_list = []
for i in range(12):
months_list.append(i+1)
# Label
label_2 = Label(master=frame, text="Month:")
label_2.grid(row=0, column=2)
# Defining variable and option menu
month_var = IntVar()
month_var.set(today.tm_mon)
month = OptionMenu(frame, month_var, *months_list)
month.grid(row=0, column=3)
# Calculating the number of days in the month (It is done only once using the set value)
days = calendar.monthrange(int(y), month_var.get())[1]
# List of options for days
days_list = []
for i in range(days):
days_list.append(i + 1)
# Label
label_3 = Label(master=frame, text="Day:")
label_3.grid(row=0, column=4)
# Defining variable and option menu
day_var = IntVar()
day_var.set(today.tm_mday)
day = OptionMenu(frame, day_var, *days_list)
day.grid(row=0, column=5)
root.mainloop()
The problem lies here:
# Calculating the number of days in the month (It is done only once using the set value)
days = calendar.monthrange(int(y), month_var.get())[1]
days_list = []
for i in range(days):
days_list.append(i + 1)
I would like this to be done any time a new month is selected. Then days_list should be used in the last option menu
I