1

I have created a small GUI in which I want to create buttons in a loop which are supposed to open a text file. The function to open the textfile stands in another python module which I have included:

def openTextEditor(textfile):
"""opens the textfile with path 'textfile' """
    [...]

In my actual loop I try to assign different paths to the buttons:

for path in foobar:
    print path
    tk.Button(text="EDIT",
              relief="tk.RAISED",
              bg='black',
              fg='white',
              command= lambda: openTextEditor(path) ).grid(row=r, column=c)

From this post I know that I have to use a lamda expression in order to pass a parameter as button command.

However, my console prints different paths to text files:

path1
path2
path3

while all of my 3 buttons open text file with path3. I assume that only the last assignment of the lambda function has effect, but why?

Community
  • 1
  • 1
h0ch5tr4355
  • 2,092
  • 4
  • 28
  • 51

0 Answers0