0

I want to create a simple interface that takes a list of student names that are in your classroom as input and outputs a row of buttons for each one,

ex:

John Johnson (Idle)(Talking)(etc)

Doris Day    (Idle)(Talking)(etc)

When each button is clicked, it writes a log of that student name and behavior to an output file.

My problem is that it currently makes every button write a log for whatever student is last on the list of student names. It is my understanding that this is due to 'late binding', but this time I can't resolve it on my own in the time I have before school starts.

currentperiod= [studentname1, studentname2, ...etc]   
def behaviorlog(name, behavior):
    write name and behavior to a spreadsheet
def idle():
    name=currentperiod[x]
    behavior='Idle'
    behaviorlog(name,behavior)


def talking():
    name=currentperiod[x]
    behavior='Talking'
    behaviorlog(name,behavior)
def late():
    name=currentperiod[x]
    behavior='Late'
    behaviorlog(name,behavior)
def rude():
    name=currentperiod[x]
    behavior='Rude'
    behaviorlog(name,behavior)
def misplaced():
    name=currentperiod[x]
    behavior='Misplaced'
    behaviorlog(name,behavior)

from tkinter import *
master= Tk()

for x in range(len(currentperiod)):


    Label(master, text=currentperiod[x]).grid(row=x, sticky = W)
    button1=Button(master, text='Idle', command=idlemaker)
    button1.grid(row=x, column=1, sticky=W, pady=4)

    button2=Button(master, text='Talking', command=talking)
    button2.grid(row=x, column=2, sticky=W, pady=4)
    button3=Button(master, text='Late', command=late)
    button3.grid(row=x, column=3, sticky=W, pady=4)
    button4=Button(master, text='Rude', command=rude)
    button4.grid(row=x, column=4, sticky=W, pady=4)
    button5=Button(master, text='Misplaced', command=misplaced)
    button5.grid(row=x, column=5, sticky=W, pady=4)

mainloop( )
Mr. H
  • 3
  • 1

2 Answers2

1

You must pass a student object (or his identifier) to the function. How to do it? This may help you to figure it out.

You initialize the variable x in the loop, than change it (by looping) and in the end it has the last value from the loop (in your case it is range(len(currentperiod)) - 1). Then, when it is used in your functions, it seems to use it as the real value while you expect it has the x value as it had in the loop.

Last note I would like to write is that you should take a look on object oriented programming. If every student was just an instance of a class and had all the methods, it might be easier for you to write and understand what is really happening.

0

You need to make sure that x has the appropriate value when the code is executed; your Button invocations bind only the function profile, not the call-time arguments. Simply pass the required value separately with each invocation.

def talking(id):
    name=currentperiod[id]
    behavior='Talking'
    behaviorlog(name,behavior)

Then you pass the student id (appropriate value of x) as part of the call. Alternately, make a Student class, so that the entire student object is passed when you click on one of the student's buttons.

Prune
  • 76,765
  • 14
  • 60
  • 81