Inserting Unicode chars in Entry widget

A

Alan Graham

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
 
C

Chris Angelico

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?

What GUI toolkit are you using?

Whatever it is, there ought to be a simple method on the Entry widget
that inserts a character. Poke around with it and you'll probably find
it, though you may find it under a name you don't expect. (Happens a
lot. GTK calls something "sensitive" when the rest of the world calls
it "enabled".)

ChrisA
 
I

Irmen de Jong

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
 
C

Chris Angelico

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
 
I

Irmen de Jong

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,045
Messages
2,570,389
Members
47,052
Latest member
ketan

Latest Threads

Top