Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Number of Entry Fields in Tkinter

I'm trying to create a widget that contains an amount of entry fields based on the an aspect of a file that's loaded in.

I've been using

self.e = Entry(self.master);
self.e.pack();
self.e.delete(0,END);
self.e.insert(0, 0);

to create each entry but ideally want to iterate on this command. Each entry variable should have a different name so I can call each individual cell, which I don't know if that is possible.

More generally, what I'm trying to do is create a n by 1 table where the user can input an integer to the cells and I can access that value in another function.

like image 336
Arger Avatar asked Oct 22 '22 07:10

Arger


1 Answers

More generally, what I'm trying to do is create a n by 1 table...

Use a list and append however many Entry widgets are needed.

Each entry variable should have a different name so I can call each individual cell

Simply index the list (of course you could set it up to create new instance variables but you probably don't actually want that).

You can even put your setup code in a function and call that each time.

def create_entry_widget(self, x):
    new_widget = Entry(self.master)
    new_widget.pack()
    new_widget.insert(0, x)
    return new_widget

All you need to do is define self.n based on your file.

self.entry_widgets = [self.create_entry_widget(x) for x in xrange(self.n)]

NOTE: Don't use semicolons ; at the end of each line in Python.

like image 181
Jared Avatar answered Oct 24 '22 11:10

Jared