2

Earlier I was having trouble with changing the state of one entry. That was solved by using .grid() on a new line but when I altered this in my program it only changed one of the entries.

from tkinter import *

def changestate():
    global entry1
    entry1['state']='normal'


root=Tk()

entry1_list=[]

def newday():
    global entry1
    row=0
    for i in range(0,5):
        var=IntVar()

        entry1=Entry(root,width=3,bd=4,textvariable=var,state='disabled')
        entry1.grid(row=row,column=1)

        entry1_list.append(var)
        row=row+1

    button1=Button(root,text='Change state',command=changestate).grid(row=row,column=1)


newday()

root.mainloop()

It is meant to change the state of all the entries to normal when the button is clicked however it only changes the last one.

I am using the same name for the entries because I don't want to type them out multiple times in my program as it would make it very long and I would not be able to let the user input the number of entries they want to appear.

Is there a way I can refer each entry1 individually so that this will work?

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Daniel
  • 43
  • 9

1 Answers1

2

You can store the entries in a list and update each one in your function:

def changestate():
    for e in entry1_list:
       e['state'] = 'normal'


root = Tk()

entry1_list = []

 def newday():
    var = IntVar()
    for i in range(5):
        entry1 = Entry(root, width=3, bd=4, textvariable=var, state='disabled')
        entry1.grid(row=i, column=1)
        entry1_list.append(entry1)
    Button(root, text='Change state', command=changestate).grid(row=i+1, column=2)

In your code entry1 is referencing the last one you create in the loop so only that gets updated.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321