I have 20 text boxes which I created using tkinter-Python as shown below:
Now I have a list y1[]
If number of elements in y1 = 6,
6 text boxes should be activated
If number of elements in y1 = 3,
3 text boxes should be activated
How do I do this?
The below is what I've tried so far:
if len(y1) <= 10:
i = 0
for i in range(len(y1)):
if i == 1:
txtbox1.config(state=NORMAL)
txtbox1.insert(0, y[0])
if i == 2:
txtbox2.config(state=NORMAL)
txtbox2.insert(0, y[0])
if i == 3:
txtbox3.config(state=NORMAL)
txtbox3.insert(0, y[0])
if i == 4:
txtbox4.config(state=NORMAL)
txtbox4.insert(0, y[0])
if i == 5:
txtbox5.config(state=NORMAL)
txtbox5.insert(0, y[0])
if i == 6:
txtbox6.config(state=NORMAL)
txtbox6.insert(0, y[0])
if i == 7:
txtbox7.config(state=NORMAL)
txtbox7.insert(0, y[0])
if i == 8:
txtbox8.config(state=NORMAL)
txtbox8.insert(0, y[0])
if i == 9:
txtbox9.config(state=NORMAL)
txtbox9.insert(0, y[0])
if i == 10:
txtbox10.config(state=NORMAL)
txtbox10.insert(0, y[0])
i = i + 1
Store the textboxes in an array and then iterate over array to enable/disable the specific number of textboxes.
import tkinter as tk
import random
N = 20
activated_text_boxes = random.randint(0, N)
window = tk.Tk()
text_boxes = [tk.Text(window, height=1, width=300) for i in range(N)]
for idx, tb in enumerate(text_boxes):
tb.pack()
print(tb)
if idx < activated_text_boxes:
print("normal")
tb["bg"] = "blue"
tb["state"] = "normal"
else:
print("disabled")
tb["bg"] = "red"
tb["state"] = "disabled"
window.mainloop()