0

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.

Ben the Coder
  • 539
  • 2
  • 5
  • 21
Gmoss2112
  • 1
  • 2
  • Please see [ask]. – Ben the Coder Mar 25 '23 at 14:36
  • You have provide only one date to `value` option of `OptionMenu`, so there should be only one item in the menu. – acw1668 Mar 25 '23 at 15:21
  • Thanks for the reply - maybe you can help when "print(d.strftime("%A %d/%m/%Y"))" i get a total of 8 days displayed. But when i add it to the OptionMenu i just get the one date displayed. How do i get all 8 ? – Gmoss2112 Mar 25 '23 at 15:51

3 Answers3

0

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()
Brener Ramos
  • 256
  • 5
  • Have you tried your code? It raises exception. – acw1668 Mar 26 '23 at 01:31
  • Hi , this code does print the dates in the list. However i need in the format the same as print which is where i am struggling. Any help is appreciated. – Gmoss2112 Mar 26 '23 at 13:35
  • w = OptionMenu(master, value=d.strftime("%A%d/%m/%Y"), variable=variable, *d) TypeError: type object argument after * must be an iterable, not datetime.datetime – pippo1980 Mar 27 '23 at 16:20
  • Sorry. For some reason I added the wrong code in the final answer. It is fixed now. Hope it helps. – Brener Ramos Mar 28 '23 at 11:08
0

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))

pippo1980
  • 2,181
  • 3
  • 14
  • 30
0

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()
...
acw1668
  • 40,144
  • 5
  • 22
  • 34