November 10th, 2013, 12:58 PM
Tkinter, japanese "kotaeri" input method does not work
(crossposting note: This has already been posted at python-forum.org, but didn't get any reply so far)
(Python 2.3.4, OSX 10.4)
For entering Japanese text, I switch my keyboard to the Japanese "kotoeri" input method (see en.wikipedia.org/wiki/Kotoeri). This doesn't work anymore, when entering the text in a tkinter Entry widget.
The problems are not the characters itself. I can switch the keyboard schema to, say, Greek, and happily enter Greek characters. I can type a Japanese text in an application outside of tkinter (Terminal command line, text editor,...), copy it and paste it into the Entry widget. This all works fine. It is just that I can't type the Japanese text directly into the widget. It is as if the "Kotoeri" input handler is bypassed.
I googled for this problem, and while I didn't find anything special for the Mac, I found one user complaining a very similar problem on Microsoft Windows, where also an input method akin to Kotoeri exists. The culprit in this case was a bug in the Tk libraries.
Is there a similar, known bug in the Tk libraries for this OSX version too?
November 10th, 2013, 04:23 PM
You would have to specify a Japanese font when creating the Entry widget so it has access to the characters to display. This code will tell you what fonts Tkinter can find (may be different from the fonts the system uses), (you can Google for the difference between font families and font names but it really doesn't matter as long as it is found). Please provide some sample code to get additional help.
## a very basic font display
import Tkinter as tk ## Python 2.x
import tkinter as tk ## Python 3.x
top = tk.Tk()
print "------------font families" +"-"*30
font_tuple = tkFont.families()
for each_font in font_tuple :
print "------------font names" +"-"*30
font_tuple = tkFont.names()
for each_font in font_tuple :
Last edited by dwblas; November 10th, 2013 at 04:37 PM.
November 11th, 2013, 01:38 AM
> You would have to specify a Japanese font when creating the Entry widget so it has access to the characters to display.
No, that's not the problem. The widget can display Japanese text without problem. Actually, I never specify any font for the widgets, and the display all characters. That's the great thing with Unicode.
I guess the widget somehow bypasses the input method of the keyboard driver, and that's why kotoeri doesn't work here. The question is, how can I make it work?
November 11th, 2013, 11:56 AM
No one wants to sit around and guess what code you are trying so this will be my last post. You may be entering characters that are not in the range of the encoding you are using. If you are using UTF-8 (we don't know) then you may be trying to enter characters in the UTF-16 range, etc.
November 15th, 2013, 06:22 AM
Sorry if I was unclear with this. I do NOT specify any encoding by myself. My understanding was, that on the operating system I'm using (OSX 10.4) the encoding is always some Unicode.
Originally Posted by dwblas
However, I didn't consider that the encoding used is a problem - after all, the entry widget happily displays all Japanese characters I need. I can also copy and paste (from, say, the Terminal command line) the Japanese text perfectly fine into the entry field. The problem is that the input method (Kotoeri) does not work in the entry widget. If it really were a problem with wrong encoding, my widgets would not show any Japanese text correctly - or am I wrong here?
My guess was that the Tkinter Entry widget bypasses Kotoeri. Maybe I draw the wrong conclusion, but at least it looks like this.
For example, when I enter the letters RE followed by a space in the entry field, it just displays "RE ", while the "usual" behaviour would be to show the letter れ, plus a selection box which would allow me to choose alternative spellings, such as レ or 列. Leaving the encoding issue aside for a moment, the fact that the selection box doesn't open automatically, is a sign that Kotoeri is not used here.
December 3rd, 2013, 06:58 AM
REASON FOUND (Tkinter, japanese "kotaeri" input method does not work)
I finally found explanations (for example here: http://comments.gmane.org/gmane.comp.lang.tcl.mac/3594) for my problem:
The older Tk versions don't support the Japanese Input method on OSX yet. In my case, it is Tk 8.4, which to the best of my knowledge is the most recent which can be used with Python 2.3.4.
I note I found on this subject (http://www.ne.jp/asahi/kazuo/sasagawa/eng.html, see the explanations of cocoEdit) seems to suggest that even Tk 8.5 and 8.6 don't have it yet.
Several sites suggest for this reason to not use Tk as a GUI, if Japanese text needs to be entered :-(
April 23rd, 2014, 03:04 AM
I found that it doesn't work better on newer Python versions either (on OSX). I had a chance to get my hands on a Windows 7 machine, and kotoeri works perfectly well with my program (using Python 2.7).
As for OSX, I also tested 10.6 (Snow Leopard) in addition to Tiger, with Python 2.6 and 3.0, and in both cases Kotoeri does NOT work in Tkinter Entry widgets.
To sum up: It works on Windows 7, but not on OSX (at least not up to Snow Leopard; can't test newer versions).