0

I am trying to create a todolist. The main concept is that I loop over a list of tasks and create labels and buttons like so

for task in todoList:
  lbl_task = tk.Label(frame, text=task)
  btn_done = ttk.Button(frame, text=task)

Then I pack them onto the frame

My question is how do I access the the name of the task associated with a specific button. I thought of creating a class the inherits ttk.Button but that didn't seem like a clean solution. I wanted to know if there is another way my problem could be solved.

Sbu_05
  • 31
  • 3
  • 1
    It's confusing, using `=task` with both widgets? ***I thought of creating a class the inherits `ttk.Button`***: You are close, compound both widgets into a own widget and implement a `.get()` which returns `Label text`. Something like this [Define your own widget](https://stackoverflow.com/a/60536050/7414759) – stovfl May 17 '20 at 21:40

1 Answers1

1
lbl_taskList=[]
btn_doneList=[]

for task in todoList:
    lbl_task = tk.Label(frame, text=task)
    btn_done = ttk.Button(frame, text=task)

    lbl_task.grid()#or use pack instead of grid
    btn_done.grid()

    lbl_taskList.append(lbl_task)
    btn_doneList.append(btn_done)

if you want to access first lbl_task for example:

lbl_taskList[0]['text']

same for btn_doneList

Georges Farah
  • 813
  • 1
  • 9
  • 15