-1

is it possible to overwrite an Entry widget?

I have more than 10 Entries widgets that they are constantly updated in a while loop and I was worrying about efficiency.

 def update_data(self):
    if(index <= 1):
        self.n_entry.insert(END, index)
    else:
        self.a_entry.delete(0, END)
        self.b_entry.delete(0, END)
        self.c_entry.delete(0, END)
        self.d_entry.delete(0, END)
        self.e_entry.delete(0, END)
        self.f_entry.delete(0, END)
        self.g_entry.delete(0, END)
        self.h_entry.delete(0, END)
        self.i_entry.delete(0, END)
        self.l_entry.delete(0, END)

        self.a_entry.insert(0, index)
        self.b_entry.insert(0, index)
        self.c_entry.insert(0, index)
        self.d_entry.insert(0, index)
        self.e_entry.insert(0, index)
        self.f_entry.insert(0, index)
        self.g_entry.insert(0, index)
        self.h_entry.insert(0, index)
        self.i_entry.insert(0, index)
        self.l_entry.insert(0, index)

Is the above solution the only way to update Entries Widgets?

Soichiro
  • 81
  • 2
  • 9
  • 1
    https://stackoverflow.com/a/35165723 This is one way. – Lafexlos Jul 26 '17 at 11:52
  • 1
    how often is constantly, i mean if it is to often than you maybe can use Label instead of Entry if you don't use entry's ability to enter text yourself + since you are inserting (0, index), i think there is no need to delete text first, you should try that way also – Dušan Atanacković Jul 26 '17 at 12:36
  • You would probably need to have thousands of entry widgets before efficiency became an issue. – Bryan Oakley Jul 26 '17 at 13:30

1 Answers1

0

Here is a simple example of how you could use a list to store all your entry fields instead.

As you can see with the below example it takes only 4 lines of code to create all the Entry widgets. The list and the for loop. The benefit to this is you can use that same 4 lines of code to create as many or as few Entry widgets or any other widget for that matter as you want.

from tkinter import *


root = Tk()

entry_list = []

for i in range(10):
    entry_list.append(Entry(root))
    entry_list[i].pack()

def update_entry_fields():
    for i in entry_list:
        i.delete(END)
        i.insert(0, "Test")

btn1 = Button(root, text="Test", command = update_entry_fields)
btn1.pack()

root.mainloop()

This example is just using a for loop to update each entry but you can also use the index as well.

You could also use a dictionary to do the same.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79