Hello. i have this problem with a program im coding in python. heres the code:

#! /usr/bin/python
from Tkinter import *
list=[".", ".", ".", ".", ".", ".", ".", ".", "."]
root=Tk()

def p(lol):
      list[0]=1
      lol["text"] = list[0]
def plade():
      
###### ROW=0     

      a1=Button(root, text=list[0], command=p(a1))
      a1.grid(row=0)
      
      a2=Button(root, text=list[1], command=p(a2))
      a2.grid(row=0, column=1)
      

      a3=Button(root, text=list[2], command=p(a3))
      a3.grid(row=0, column=2)
##### ROW=1



plade()
root.mainloop()

and heres the output:

Traceback (most recent call last):
  File "./kryds", line 26, in <module>
    plade()
  File "./kryds", line 13, in plade
    a1=Button(root, text=list[0], command=p(a1))
UnboundLocalError: local variable 'a1' referenced before assignment

Could you guys please help me figure this out, and explain the solution?

Difficult to understand your goal, but here is something running end result of playing with it:

#! /usr/bin/python
from Tkinter import *
from functools import partial

def p(lol):
      buttons[lol]["text"] = 'Hi from %i!' % lol

def plade(root):
    for n in range(0,4):
      buttons[n] = Button(root, text= 'Button%i' % n, command=partial(p, n))
      buttons[n].grid(row=0, column=n)

buttons=["." for _ in range(4)]
root=Tk()
plade(root)
root.mainloop()

Edited 5 Years Ago by pyTony: n/a

This question has already been answered. Start a new discussion instead.