python / cpython

The Python programming language
https://www.python.org
Other
62.87k stars 30.12k forks source link

Unicode problem in Tkinter under Windows #34398

Closed 50eff062-408a-4098-b1b2-8222303b9d0c closed 22 years ago

50eff062-408a-4098-b1b2-8222303b9d0c commented 23 years ago
BPO 418173
Nosy @malemburg, @loewis

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = created_at = labels = ['expert-tkinter'] title = 'Unicode problem in Tkinter under Windows' updated_at = user = 'https://bugs.python.org/anonymous' ``` bugs.python.org fields: ```python activity = actor = 'loewis' assignee = 'effbot' closed = True closed_date = None closer = None components = ['Tkinter'] creation = creator = 'anonymous' dependencies = [] files = [] hgrepos = [] issue_num = 418173 keywords = [] message_count = 4.0 messages = ['4489', '4490', '4491', '4492'] nosy_count = 4.0 nosy_names = ['lemburg', 'nobody', 'loewis', 'effbot'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue418173' versions = [] ```

3772858d-27d8-44b0-a664-d68674859f36 commented 23 years ago

Unicode problem in Tkinter under Windows 2000 Keyboard-entered chars in ascii range > 128 mess up internal unicode encoding in text-widget leading to unicode errors

The following example should reproduce the bug:

>> import Tkinter >> t=Tkinter.Text() >> t.pack() >> t.insert("1.0",u'\xe2\xee\xfb')

Now set the focus to the text-widget and via the 
keyboard enter an a umlaut into the text-widget 
(alternatively press ALT and enter 0228 on the Numpad 
of your Keyboard to simulate this)
Then test the result:
>>> t.get("1.0","end")
u'\xe2\xee\xfb\xe4\n'
This is what you get under Linux (I was told) and what 
it should be.
However, under Windows 2000 I get:
'\xc3\xa2\xc3\xae\xc3\xbb\xe4\n'
which is a mixture of UTF-8 and cp1252(?) leading to 
an Unicode-error, if I try e.g. to save it as a file.
(All characters of an 8-bit value > 128 (e.g. latin-1 
or cp1252) entered via keyboard into a text-widget 
cause such a weird behaviour, not just the a umlaut.)

A simple workaround (not thoroughly tested) could look like this:

def badkey(self, event):
    try:
        if ord(event.char) > 127:
            txt.insert("insert", unicode
(event.char,"cp1252"))
            return "break"
    except:
        pass

txt being the instance of a text-widget, that is bound to a callback for the key-press-event:

if sys.platform == "win32":
         txt.bind("<KeyPress>",badkey)
malemburg commented 23 years ago

Logged In: YES user_id=38388

I believe that this is a TCL bug. Could someone with more Tcl/tk knowledge please review this ? If it is Unicode related, then you can assign it back to me.

Thanks.

3772858d-27d8-44b0-a664-d68674859f36 commented 22 years ago

Logged In: NO

This is a directory organization bug of Python. Try and move "tcl8.3\" from "\Python2?\tcl\" to "\Python2?\Lib\".

For details, see Request ID 474505 "Tkinter and its encodings on Windows" on the patch list. It is a rather well-known tip among Japanese pythoneers.

61337411-43fc-4a9c-b8d5-4060aede66d0 commented 22 years ago

Logged In: YES user_id=21627

This is fixed with FixTk.py 1.6.