import tkinter as tk
class App(tk.Frame):
def __init__(self):
super().__init__()
self.pack()
self.buttons = []
self.entries = []
You will want to use lambda here to keep the correct index value for each of the entry fields.
You can use the value of n_row
to keep track of what button links to what entry.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.entries = []
for n_row in range(20):
self.entries.append(tk.Entry(self))
self.entries[-1].grid(row=n_row, column=0, pady=(10,10))
tk.Button(self, text='disable', command=lambda i=n_row: self.disable_entry(i)).grid(row=n_row, column=1)
def disable_entry(self, ndex):
self.entries[ndex]['state'] = 'disabled'
if __name__ == '__main__':
App().mainloop()
For switching between both states you can use the same method to check if the state is one thing and then change it to another.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.entries = []
self.buttons = []
for n_row in range(20):
self.entries.append(tk.Entry(self))
self.buttons.append(tk.Button(self, text='disable', command=lambda i=n_row: self.toggle_state(i)))
self.entries[-1].grid(row=n_row, column=0, pady=(10, 10))
self.buttons[-1].grid(row=n_row, column=1)
def toggle_state(self, ndex):
if self.entries[ndex]['state'] == 'normal':
self.entries[ndex]['state'] = 'disabled'
self.buttons[ndex].config(text='Enable')
else:
self.entries[ndex]['state'] = 'normal'
self.buttons[ndex].config(text='Disable')
if __name__ == '__main__':
App().mainloop()