Inserting Unicode chars in Entry widget

Irmen de Jong irmen.NOSPAM at xs4all.nl
Sat Dec 29 20:15:12 CET 2012


On 29-12-2012 18:23, Chris Angelico wrote:
> On Sun, Dec 30, 2012 at 4:11 AM, Irmen de Jong <irmen.NOSPAM at xs4all.nl> wrote:
>> b1=Button(f, text='char1', command=lambda b=1: insert_char(b))
>> b2=Button(f, text='char2', command=lambda b=2: insert_char(b))
>> ...etc..
>>
>> def insert_char(b):
>>     if b==1:
>>         entrywidget.insert(0, u"\u20ac")   # inserts € in the entry widget e
>>     elif b==2:
>>         entrywidget.insert(0, ...some other char...)
>>     ...
> 
> I'm not familiar with tkinter syntax, but why not:
> 
> b1=Button(f, text='char1', command=lambda: insert_char(1))
> b2=Button(f, text='char2', command=lambda: insert_char(2))
> 
> or even:
> 
> b1=Button(f, text='char1', command=lambda: insert_char(u"\u20ac"))
> b2=Button(f, text='char2', command=lambda: insert_char("... some other
> char..."))
> 
> Seems weird to multiplex like that, but if there's a good reason for
> it, sure. I'm more of a GTK person than tkinter, and more of a
> command-line guy than either of the above.
> 
> ChrisA
> 

You're right there's nothing special about tkinter there, I was copying some existing
code a bit too literally. Simplify the lambdas as needed. :)

Irmen




More information about the Python-list mailing list