1

I am trying to create a GUI that allows you to pick a date from a calendar. I have made a 'calendar' of buttons that aligns themselves according to the current month. I want to be able to set the callback to each of these buttons to return their specific data, which is as simple as the day # they display... But since the buttons are created within a generator, the integers that were used to create them are transient...

Code below, the callback function just returns the same number. Using Python 3.3.

import datetime
from tkinter import *
import calendar

today = datetime.date.today()
calendar.setfirstweekday(6)
GUI = Tk()
monthChoiceContainer = Frame(GUI)
monthChoiceContainer.pack()
monthChoice = Label(monthChoiceContainer, text=today.strftime("%B"))
monthChoice.grid(row=0, column=1)
calendarContainer = Frame(GUI)
calendarContainer.pack()
def callback(data):
    print(data)
dayButton = { }
dayLabel = { }
cnth = 0
for items in calendar.weekheader(2).split(" "):
    dayLabel[cnth] = Label(calendarContainer, text=items)
    dayLabel[cnth].grid(row=0, column=cnth)
    cnth+=1
cnth = 0
for lists in calendar.monthcalendar(today.year, today.month):
    for items in lists:
        if items !=0:
            dayButton[cnth] = Button(calendarContainer, text=str(items), width=2,
                                     command=lambda: callback(cnth))
            dayButton[cnth].grid(row=cnth//7+1, column=cnth%7)
        cnth+=1
GUI.mainloop()

Is there a way I can make it so the callback function returns individual data related to each button?

houallet
  • 249
  • 3
  • 10

0 Answers0