[tkinter] question about correct use of validation

Peter Otten __peter__ at web.de
Thu Jan 17 16:46:31 EST 2019


steve wrote:

> Il 17/01/19 14:08, Peter Otten ha scritto:
> 
>> Two remarks:
>> 
>>>           self.id1_entry = self.make_entry(self.parent, maxlen=1).pack()
>> 
>> You set all idX_entry attributes to None, as that's what pack() returns.
> 
> you mean..
> 
> self.idx_entry = self.make_entry(self.parent, width=50, maxlen=30,
> highlightcolor='blue', validate='key') ?
> 
> or
> 
> self.idx_entry.pack() ?
> 
> I did not understand :-D

Both. This

> self.id1_entry = self.make_entry(self.parent, maxlen=1).pack()

will assign None to self.id1_entry. You need two steps

self.id1_entry = self.make_entry(...)  # create widget
self.id1_entry.pack()                  # layout widget

Alternatives:

- If you know that you'll always just call pack() you can change the 
make_entry() method accordingly

def make_entry(...):
    ...
    entry.pack()
    return entry


- Write a helper function

def pack(widget):
    widget.pack()
    return widget

and then use it

self.id1_entry = pack(self.make_entry(...))  # create and layout widget





More information about the Python-list mailing list