I am trying to make a program that adds one month to a date variable every time the user presses a button.
Here's my code:
import datetime
from dateutil.relativedelta import relativedelta
from tkinter import *
gameDate = datetime.datetime(1985, 9, 1)
def nextMonth(gameDate, worldDateLabel):
gameDate = gameDate + relativedelta(months=1)
worldDateLabel.config(text=gameDate.strftime("%B %Y"))
return gameDate
window = Tk()
worldDateLabel = Label(window, text=gameDate.strftime("%B %Y"))
worldDateLabel.grid(row=0, column=0)
next_btn = Button(window, text="Next Month", command=lambda:nextMonth(gameDate, worldDateLabel))
next_btn.grid(row=1, column=0)
window.mainloop()
The date updates the first time I click on the button but then it stays to October and doesn't move to the next month.
I think it's because the program updates only the local variable inside the function instead of the main gameDate one. I tried to fix that with the return at the end but that doesn't help.
I am not sure what I am doing wrong…
P.S.: I am trying not to use global.