1

My application is a form which populates its fields from a database. I am in the process of writing the function that navigates between these records. I am having problems writing my "next record" function.

At any given time, the "Current Record" showing on the screen is in a variable current_record.

def next_record(current_record):
    current_index = current_record.index
    current_record = Competitor(competitors[current_index + 1], current_index + 1)
    print(current_record.index)
    populate_form(current_record)

And my button calling this function:

action_button_6 = tkinter.Button(group_buttons, text='>>', width=5,
                                 command=lambda: next_record(current_record))

Although the function does its job and loads the new record into the form, it does not reassign "current_record" to the new competitor as show in the third line of the function.

How can I modify the variable from within this function?

4
  • current_record in next_record is local variable and it have nothing to do with current_record in lambda. If you assign new element to current_record in next_record you lose contact with original record. Commented Nov 28, 2015 at 15:17
  • I know I have made life a little bit more difficult for myself by naming them all the same. My question is how can I modify the variable passed to the function in lambda? Commented Nov 28, 2015 at 15:18
  • You have to change valued in existing element if you can - like current_record.some_variable = competitors[current_index + 1] and current_record.index = current_index + 1. Commented Nov 28, 2015 at 15:20
  • so change the index of the current instance and then execute some internal class function to re-initialise? Commented Nov 28, 2015 at 15:22

1 Answer 1

0

Unless you explicitly declare a variable that you modify in a function global, it defaults to being local even if there is a global variable with the same name, thus current_record is a local variable. Adding global current_record to the beginning of the next_record function and renaming the function argument to old_record would fix that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.