How do I link python tkinter widgets created in a for loop? -
i want create button , entry(state=disabled) widgets loop. number of widgets created runtime argument. want every time click button, corresponding entry become enabled(state="normal"). problem in code button click, affects last entry widget. there anyway fix this.? here code:
from tkinter import * class practice: def __init__(self,root): w in range(5): button=button(root,text="submit", command=lambda:self.enabling(entry1)) button.grid(row=w,column=0) entry1=entry(root, state="disabled") entry1.grid(row=w,column=1) def enabling(self,entryy): entryy.config(state="normal") root = tk() = practice(root) root.mainloop()
few issues in code -
you should keep
buttons
, entries creating , save them in instance variable, store them in list ,w
index each button/entry in list.when
lambda: something(some_param)
- function value ofsome_param()
not substituted, till when function called, , @ time, working on latest valueentry1
, hence issue. should not depend on , rather should usefunctools.partial()
, send in index ofbutton/entry
enable.
example -
from tkinter import * import functools class practice: def __init__(self,root): self.button_list = [] self.entry_list = [] w in range(5): button = button(root,text="submit",command=functools.partial(self.enabling, idx=w)) button.grid(row=w,column=0) self.button_list.append(button) entry1=entry(root, state="disabled") entry1.grid(row=w,column=1) self.entry_list.append(entry1) def enabling(self,idx): self.entry_list[idx].config(state="normal") root = tk() = practice(root) root.mainloop()
Comments
Post a Comment