1

An array of Entries was created using the following code

from tkinter import *
root = Tk()

height = 5
width = 5

delta=0

for i in range(height): #Rows
  for j in range(width): #Columns
    b = Entry(root, text="",width=8)
    b.grid(row=i, column=j)

mainloop()

How do I access each Entry to update its value ( using StringVar - for example ) ?

5
  • 1
    Why not store each entry in a list? Commented Jul 9, 2017 at 12:58
  • @SamChats A GUI is updated every few seconds Commented Jul 9, 2017 at 12:59
  • Sorry, I miss your point. Does that imply you can't use lists? Commented Jul 9, 2017 at 13:00
  • Perhaps I miss yours. Can you explain? Commented Jul 9, 2017 at 13:01
  • 1
    Instead of just b = Entry(root, text="",width=8), you can do something like entries.append(b) after that line and then access each entry by doing entries[3].do_something(). Commented Jul 9, 2017 at 13:06

2 Answers 2

5

You could create a list of lists for your Entry widgets.

from tkinter import *
root = Tk()

height = 5
width = 5

delta=0

entries = []

for i in range(height): #Rows
  newrow = []
  for j in range(width): #Columns
    b = Entry(root, text="",width=8)
    b.grid(row=i, column=j)
    newrow.append(b)
  entries.append(newrow)

mainloop()

You could then address individual entries as e.g. entries[2][4].

Edit: To edit the text of entry widget e, first use e.delete(0, END) to clear it, and then use e.insert(0, "new text") to insert new text.

Edit2: Alternatively, you could store the StringVars in a list of lists instead of the widgets...

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for answers. I used your code, and it worked OK, but since it is not addressing StringVar, I didn't succeed to add text after creation of GUI
You don't necessarily require stringvars. You can edit the contents of the Entry widget as shown in the updated answer.
0

You need to first declare the StringVar variable:

myvar = StringVar()

Then in your loop whenever you want to check to content of the variable use the get() method.

x = myvar.get()

Now x will hold the value. You can also perform a bool test with if

if myvar.get():
     print(myvar.get())

In that if statement the program checks if there is data in the var. If not it will move on

Looking at it again you should also declare the StringVar() in your button. Like so:

b = Button(text='clickme', texvariable=myvar)

Look Here for more info

8 Comments

How can change the value of cell 5,1 for example?
What do you mean? It is hard to say with out knowing what your trying to accomplish. The user interacting with the gui will change the value right? The get method just updates the value that the user entered
I'll try to explain: I create a 5X5 Entry matrix in a GUI ( I could use Label as well for that purpose, but for later use i prefer using Entry ). I wish to display on GUI my 5 TASKS ( columns ) and their status: enable/ disable, on-time, off- time, device to operate etc... every second those values inside Entries are changed ... that is what I want to display
What changes those values? If the change is not coming from a user but rather from software you won't be using the entry widget.
it can be a physical button that changes on.off state or a scheduled task manager. the GUI (this part of it )will display those state and counters.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.