Inserting Unicode chars in Entry widget

Irmen de Jong irmen.NOSPAM at xs4all.nl
Sat Dec 29 12:11:22 EST 2012


On 29-12-2012 17:43, Alan Graham wrote:
> Hello Python experts,
> 
> I want to insert Unicode chars in an Entry widget by pushing on buttons;
> one for each Unicode character I need. I have made the Unicode buttons.
> I just need a simple function that will send the Unicode character to
> the Entry widget.
> Is there a better approach?
> 
> Alan
> 

Not sure what the question is. A better approach to doing what?

I assuming you're doing tkinter (it is helpful if you mention the toolkit when posting a
question). I'd create a function that you bind to all 'unicode buttons', and let the
function insert the correct character depending on which button triggered it.

A possible way to do that is to use a lambda with a different parameter for every
button, like this:

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...)
    ...


Or simply define a different command function for every button, then you don't have to
use the lambda.

-irmen




More information about the Python-list mailing list