Only one selection showing in drop down list

67 views Asked by At

I am trying to display seven days from today's date in a Tkinter option menu, but when I run the script only one day gets printed.

When I print as per the code all seven days show in print but not on Tkinter.

from tkinter import *
from datetime import datetime, timedelta

master = Tk()

#Gets date from host computer and prints 7 days
date = datetime.now()
for x in range(7):
   d = date - timedelta(days=x)
   print(d.strftime("%A %d/%m/%Y"))


#Add the date to the tkinter window
variable = StringVar(master)
variable.set("Select Date") # default value
w = OptionMenu(master, value=d.strftime("%A%d/%m/%Y"),variable= variable)
w.pack()

mainloop()

Using Python 3.

3

There are 3 answers

4
Brener Ramos On

You need to store all computed values of d and pass them to OptionMenu.

from tkinter import *
from datetime import datetime, timedelta

master = Tk()

# Gets date from host computer and prints 7 days
date = datetime.now()
dates = []
for x in range(7):
    d = date - timedelta(days=x)
    dates.append(d.strftime("%A %d/%m/%Y"))
    print(d.strftime("%A %d/%m/%Y"))


# Add the date to the tkinter window
variable = StringVar(master)
variable.set("Select Date")  # default value
w = OptionMenu(master, variable, *dates)
w.pack()

mainloop()
0
pippo1980 On

try:


from tkinter import *
from datetime import datetime, timedelta

master = Tk()

# Gets date from host computer and prints 7 days
date = datetime.now()
dates = []
for x in range(7):
    d = date - timedelta(days=x)
    dates.append(d)
    print(d.strftime("%A %d/%m/%Y"))


# Add the date to the tkinter window
variable = StringVar(master)
variable.set("Select Date")  # default value
w = OptionMenu(master, variable, *dates)
w.pack()

mainloop()


and this version too:

from tkinter import *
from datetime import datetime, timedelta

master = Tk()

# Gets date from host computer and prints 7 days
date = datetime.now()
dates = []
for x in range(7):
    d = date - timedelta(days=x)
    dates.append(d)
    print(d.strftime("%A %d/%m/%Y"))


def display_selected(choice):
    choice = variable.get()
    print('selected is : ', choice, type(choice))




# Add the date to the tkinter window
variable = StringVar(master)
variable.set("Select Date")  # default value
w = OptionMenu(master, variable, *dates, command=display_selected)
w.pack()

mainloop()

check the difference commenting out choice = variable.get()

in :

def display_selected(choice):
    choice = variable.get()
    print('selected is : ', choice, type(choice))

in the output of:

print('selected is : ', choice, type(choice))

0
acw1668 On

You need to save the dates in a list and pass this list to OptionMenu instead:

...
#Gets date from host computer and prints 7 days
date = datetime.now()
dates = []
for x in range(7):
    d = date - timedelta(days=x)
    dates.append(d.strftime("%A %d/%m/%Y"))
    print(dates[-1])

# Add the dates to the tkinter OptionMenu
variable = StringVar(master, value="Select Date")
w = OptionMenu(master, variable, *dates)
w.pack()
...