0

Yeah, so as the question states the function doesn't work with buttons, here's the code: Just for context the loop's prepose is to display each element in the 2d array stud, which stores data like this [[name, (p, class description), (a, class description][name2...] It's an attendance thing. Anyway the problem is specifically with the functions Yeas, Neas and but_yes, but_no along with the code following that.

def work():
        num = int(len(stud[0]))
        z = ent.get()
        if num <= 25:
            k = alph[num]
        else:
            m = int(num / 25)
            k = str(alph[m - 1]) + str(alph[(num - 25 * m) - 1 * m])
        for i in range(0, len(stud)):
            Lable_l = Label(root, text=("Is " + stud[i][0] + " present?"))
            Lable_l.grid(row=(9 + i), column=0)

            def Yeas(a):
                stat = str(a)
                f = k + str(i + 2)
                sheet_atten[k + str(1)] = z
                stud[i].append([stat, z])
                sheet_atten[f].value = stat
                print(stud)
            but_yes = Button(root, text="Yes", command=lambda: Yeas('p'))
            but_yes.grid(row=(9 + i), column=1)
            but_no = Button(root, text="No", command=lambda: Yeas('a'))
            but_no.grid(row=(9 + i), column=2)

            print(stud)
            workbook_atten.save(filename=nam_atten)

This is only a small excerpt from the rest that i didn't want to bore you with. This is only the latest iteration and ig i just need to rewrite the function around the button. I have tried in ways where it accepts a or p as a parameter where I input it with lambda. I have tried the make the if statement part of the code, so it just executes it when i push the button, no luck. Fir whatever it's worth, I have not been able to solve the problem, but only fix it to a point where it loops to the end of the stud array to execute, meaning i can only change the last element, not what I want. I know it's probably something basic but tkinter makes my head hurt and nothing I try seems to fix it, any help is appreciated.

  • 1
    Your `Yeas()` and `Neas()` functions are pointless - they're just setting a *local variable* that immediately disappears, which is completely unrelated to the variable of the same name in `work()`. Even if you added a `nonlocal` declaration to those functions, they would all be accessing the *same* variable, so there would be no point in having multiple instances of each type of button. Your whole design is unworkable, you cannot test the variable immediately after creating the buttons, because there is no possibility of the button having been clicked yet! – jasonharper Mar 15 '22 at 13:10
  • So sorry, this was one of my later implementations to try to solve the issue which i mentioned later. originally the problem was that it was looping till the end of the array, so i could only edit the last element. the function looked like this. https://docs.google.com/document/d/13j4bhZ1Gie-iZ9M1HMFd8mL4Tv9AeEPUNa28PDFIeNc/edit?usp=sharing – Anurag Senapaty Mar 15 '22 at 13:33
  • 1
    Please [edit] your question if the code in the question isn't actually the code you're asking about. – Bryan Oakley Mar 15 '22 at 13:59

0 Answers0