0

I'm working on making a front end of a database with python tkinter. To display the records, I use a for loop for fill out each row. In each row I'm trying to add a button that would open to that record information, but in every row's button would open to the last record. So that would indicate that the command for each button is being over written to the last value this could also mean that the buttons are not unique. I would like help in trying to generate a unique button for each loop or a solution to the instruction for the command from being over written.

list1 = ["t1", "t2", "t3"]
dcount=0
sizel=len(list1)
for x in range(0,sizel):
    button = Button(frame, text="test", command=lambda:action(frame,list1[x]))
    button.grid(row=dcount,column=0)
    dcount=dcount+1

Any help would be appreciated. I did see some solutions where they put values in front of lambda but I would not manage to get that to work.

2

3 Answers 3

0
list1 = ["t1", "t2", "t3"]
dcount=0
sizel=len(list1)
for x in range(0,sizel):
    y=functools.partial(action,frame,list1[x])
    button = Button(frame, text="test", command=y)
    button.grid(row=dcount,column=0)
    dcount=dcount+1

Sorry again if I wasted poeple's time. This is the solution i found. this post sparked the solution:How to pass arguments to a Button command in Tkinter?

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

Comments

0

Following example worked for me.

import tkinter as tk

def func1():
    print(1)

def func2():
    print(2)

def func3():
    print(3)

root = tk.Tk()
funcList = [func1, func2, func3]
buttons = []

for x in range(len(funcList)):
    buttons.append(tk.Button(root, text="test", command=funcList[x]))
    buttons[x].grid(row=x, column=0)

root.mainloop()

1 Comment

This is very interesting, I did not consider using a list for the buttons. I will also try this one out. Thank you so much!
0

Here is the simplest least-impacting change in your code:

button = Button(frame, text="test", command=lambda idx=x: action(frame, list1[idx]))

The problem was that you defined your lambda to take x, but it would take the current value of x, i.e. the last one. Instead, you need to specify that your parameter idx (or whatever you like) must take the value of x it sees during this specific loop iteration.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.