Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unicode character support on buttons #5

Open
ukBaz opened this issue Jan 7, 2017 · 1 comment
Open

Unicode character support on buttons #5

ukBaz opened this issue Jan 7, 2017 · 1 comment

Comments

@ukBaz
Copy link
Contributor

ukBaz commented Jan 7, 2017

I thought it might be helpful to share my learnings about getting unicode characters to appear (or not) on buttons. It might be good to include an example in the documentation. (Or maybe just have this ticket to refer people to).

I wanted to have arrows on my buttons so sourced the unicodes from:
http://www.w3schools.com/charsets/ref_utf_arrows.asp
To get this to appear on a button you do the following:

button2 = PushButton(box_obj, forward, text=u'\u2191', grid=[0, 1])

I also have got a noise and mute button on my GUI. I found some suitable unicode which works in Python.
For example:

~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> u'\U0001F508'
'🔈'
>>> u'\U0001F507'
'🔇'

However this doesn't work in the tkinter as it appear tcl doesn't supprt UTF-16 so I get the following error:

$ python3 python/guizero/examples/bitbot_example.py 
scanning...
Traceback (most recent call last):
  File "python/guizero/examples/bitbot_example.py", line 97, in <module>
    _build_horn_section(buz_btns)
  File "python/guizero/examples/bitbot_example.py", line 77, in _build_horn_section
    buzz_on = PushButton(box_obj, bitbot.buzzer_on, text=u'\U0001F508', grid=[0, 0])
  File "/home/pi/python/guizero/guizero/PushButton.py", line 38, in __init__
    self.config(text=text, command=command, pady=pady, padx=padx)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1322, in configure
    return self._configure('configure', cnf, kw)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1313, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: character U+1f508 is above the range (U+0000-U+FFFF) allowed by Tcl
@carlosperate
Copy link
Contributor

Coincidentally I was looking into this type of thing the other day. As you've mentioned, and as far as I understand, tcl does not support UTF-16, but you can still use UTF-8, so you'd have to convert your characters to that format:

from tkinter import Tk, messagebox
root = Tk()
root.tk.call('encoding', 'system', 'utf-8')
messagebox.showinfo("UTF-8 example", u'\ud83d\udd08')

The documentation left a lot to be desired in this area. On first impressions it sounded like it should be able to support 16-bit Unicode by default: https://www.tcl.tk/man/tcl/TclCmd/encoding.htm#M10

However it seems like their 16 bit implementation is UCS-16: https://en.wikibooks.org/wiki/Tcl_Programming/Internationalization#Unicode_implementations:_UTF-8.2C_UCS-16
Which I personally don't know much about. Technically speaking UTF-16 derived from UCS-2, but I honestly don't know where this "UCS-16" comes into play.
Funnily enough internally it is declared as "unicode" so trying root.tk.call('encoding', 'system', 'ucs-16') won't work and you'll have to use root.tk.call('encoding', 'system', 'unicode') instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants