0

I'm not entirely sure how to phrase my question in one sentence. I am using python to create a calendar with the ability to log mileage for athletes. I have a calendar made that contains a 7x4 grid containing the number of the month in the upper left corner and a button in the center that says "log today's workout."

The button is supposed to open a new window, allow for the user to enter mileage ran and how quickly, and when the user presses "log" at the bottom of the new window, it should display the mileage and pace on the day that the button was pressed.

My problem is that I cannot figure out how to replace only the specific day that was clicked with the information. because I didn't want to make a button for every day of the month, I have the same button (along with the same command) in every day. I need the button to know where in the grid it is, and be able to tell the label where to be placed with the mileage and pace.

I have tried researching lambda to see if it would help, but to no avail. here are relevant bits of my code (still fairly new to python, probably a bit sloppy, I apologize).

 count = 0      #Code for button on every day in the month
    dayCounter = numDays[0]
    rowCount = 3
    while (numDays[1] > count):
        count = count + 1
        logButton = Button(self, text=("Log Today's Workout"), command = self.log)
        logButton.grid(column=dayCounter, row=rowCount)
        if dayCounter == 6:
            rowCount = rowCount + 1   
        if dayCounter <= 5:
            dayCounter = dayCounter + 1
        else:
            dayCounter = 0



def calculate(self): 
    displayPace = Label(self, text= paceMin + ":" + formattedSec + " a mile.")
    displayPace.grid(column=???, row=???)

I have omitted a lot of code. What is displayed is the code to put a button on every day, and the code to place the pace on the calendar. I have tried some things to place in row and column. I mostly get error messages, or it places the same label in every single box. I need to know how to change the button or what to put in row and column to only replace the button clicked. If anything else is needed I'll be checking this very frequently and will update often.

  • 1
    Please edit your code down to an [MCVE](http://stackoverflow.com/help/mcve) and tell us exactly what you're trying to do, how you are doing it, and what is actually happening. – TigerhawkT3 Apr 27 '16 at 23:53
  • Sorry about that, first question I've asked. is the new question better? @TigerhawkT3 – Jeremy Breedlove Apr 28 '16 at 00:11
  • In `while (numDays[1] > count)`, where do you change either `numDays[1]` or `count`? And you may be looking for something like [this](http://stackoverflow.com/questions/7546285/creating-lambda-inside-a-loop). – TigerhawkT3 Apr 28 '16 at 00:23
  • @TigerhawkT3 `numDays[1]` comes from importing the current date and using `monthrange` to get the number of days in the current month. `count` is changed in the line directly under while, `count = count + 1` – Jeremy Breedlove Apr 28 '16 at 02:31

2 Answers2

0

With the little code provided, it is difficult to give you a working solution; here is the approach I would take:

def log(self, event):
    x, y = event.x, event.y
    date_ = self._get_date_from_canvas_location(x, y)
    self.log_a_run(date_)

def _get_date_from_canvas_location(self, x, y):
    """returns the date corresponding to the canvas location clicked
    """
    # do the job
    return date_corresponding_to_that_location

def log_a_run(self, date_):
    """capture and save the run of of the date_
    """
    # do the job
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

Using the above post and this one and some research on how events worked (I'd never heard of them), I came up with this:

grid_info = event.widget.grid_info()
self.displayRow = grid_info["row"]
self.displayColumn = grid_info["column"]

logButton = Button(self, text=("Log Today's Workout"))
logButton.grid(column=dayCounter, row=rowCount
logButton.bind('<Button-1>', self.log)

displayPace = Label(self, text= paceMin + ":" + formattedSec + " a mile.")
displayPace.grid(column=self.displayColumn, row=self.displayRow)
Community
  • 1
  • 1