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 -

  1. you should keep buttons , entries creating , save them in instance variable, store them in list , w index each button/entry in list.

  2. when lambda: something(some_param) - function value of some_param() not substituted, till when function called, , @ time, working on latest value entry1 , hence issue. should not depend on , rather should use functools.partial() , send in index of button/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

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -