How to have non-ASCII text on Text widget (Tkinter)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saito200
    New Member
    • Jan 2014
    • 2

    How to have non-ASCII text on Text widget (Tkinter)

    Hello all,

    I am making a small GUI with tkinter. On it I have a Text widget, that should be able to accept input text from the user. The problem is that when the input text is not-ASCII (e.g. ááááá) the program crashes with the error below.

    Could anybody please help me to see how to solve it? Thank you.

    Below I paste a simple code that can be used for testing, and the error that I get.

    As you can see non-ASCII text can be inserted with the insert method, but as soon as it receives non-ASCII text from the user in the GUI, it crashes.

    CODE-----
    Code:
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    from Tkinter import *
    import tkMessageBox
    import sys
    
    class App(object):
        def __init__(self,parent):
    
    # Frame
            fSize = 10
            self.f = Frame(parent,width=fSize,height=fSize)
            self.f.pack(padx=15,pady=15)
    
            self.tTale = Text(self.f, height=15, width=30, bg="grey")
            self.tTale.pack()
            self.tTale.insert(END, u'blá')
    
    #exit button
            Button(text="quit", command=quit) .pack(side=BOTTOM)
    
    root = Tk()
    root.title('Test')
    root.call('encoding', 'system', 'utf-8')
    app = App(root)
    
    root.mainloop()
    ERROR-----
    2014-02-03 23:20:03.513 Python[82405:1107] An uncaught exception was raised
    2014-02-03 23:20:03.524 Python[82405:1107] -[__NSCFConstantS tring characterAtInde x:]: Range or index out of bounds
    2014-02-03 23:20:03.525 Python[82405:1107] (
    0 CoreFoundation 0x00007fff8f597 41c __exceptionPrep rocess + 172
    1 libobjc.A.dylib 0x00007fff8e1b4 e75 objc_exception_ throw + 43
    2 CoreFoundation 0x00007fff8f597 2cc +[NSException raise:format:] + 204
    3 CoreFoundation 0x00007fff8f47e bcb -[__NSCFString characterAtInde x:] + 91
    4 Tk 0x00000001083b9 f5d TkpInitKeymapIn fo + 825
    5 Tk 0x00000001083bf ecb Tk_MacOSXSetupT kNotifier + 861
    6 Tcl 0x000000010829b ccd Tcl_DoOneEvent + 311
    7 _tkinter.so 0x0000000108219 4a3 init_tkinter + 4450
    8 Python 0x0000000107f34 14d PyEval_EvalFram eEx + 8080
    9 Python 0x0000000107f32 093 PyEval_EvalCode Ex + 1641
    10 Python 0x0000000107f38 8c8 _PyEval_SliceIn dex + 929
    11 Python 0x0000000107f35 4d4 PyEval_EvalFram eEx + 13079
    12 Python 0x0000000107f32 093 PyEval_EvalCode Ex + 1641
    13 Python 0x0000000107f31 a24 PyEval_EvalCode + 54
    14 Python 0x0000000107f50 c2c PyParser_ASTFro mFile + 306
    15 Python 0x0000000107f50 cd3 PyRun_FileExFla gs + 137
    16 Python 0x0000000107f50 821 PyRun_SimpleFil eExFlags + 718
    17 Python 0x0000000107f61 363 Py_Main + 2995
    18 libdyld.dylib 0x00007fff8be89 5fd start + 1
    )
    2014-02-03 23:20:03.526 Python[82405:1107] *** Terminating app due to uncaught exception 'NSRangeExcepti on', reason: '-[__NSCFConstantS tring characterAtInde x:]: Range or index out of bounds'
    *** First throw call stack:
    (
    0 CoreFoundation 0x00007fff8f597 41c __exceptionPrep rocess + 172
    1 libobjc.A.dylib 0x00007fff8e1b4 e75 objc_exception_ throw + 43
    2 CoreFoundation 0x00007fff8f597 2cc +[NSException raise:format:] + 204
    3 CoreFoundation 0x00007fff8f47e bcb -[__NSCFString characterAtInde x:] + 91
    4 Tk 0x00000001083b9 f5d TkpInitKeymapIn fo + 825
    5 Tk 0x00000001083bf ecb Tk_MacOSXSetupT kNotifier + 861
    6 Tcl 0x000000010829b ccd Tcl_DoOneEvent + 311
    7 _tkinter.so 0x0000000108219 4a3 init_tkinter + 4450
    8 Python 0x0000000107f34 14d PyEval_EvalFram eEx + 8080
    9 Python 0x0000000107f32 093 PyEval_EvalCode Ex + 1641
    10 Python 0x0000000107f38 8c8 _PyEval_SliceIn dex + 929
    11 Python 0x0000000107f35 4d4 PyEval_EvalFram eEx + 13079
    12 Python 0x0000000107f32 093 PyEval_EvalCode Ex + 1641
    13 Python 0x0000000107f31 a24 PyEval_EvalCode + 54
    14 Python 0x0000000107f50 c2c PyParser_ASTFro mFile + 306
    15 Python 0x0000000107f50 cd3 PyRun_FileExFla gs + 137
    16 Python 0x0000000107f50 821 PyRun_SimpleFil eExFlags + 718
    17 Python 0x0000000107f61 363 Py_Main + 2995
    18 libdyld.dylib 0x00007fff8be89 5fd start + 1
    )
    libc++abi.dylib : terminating with uncaught exception of type NSException
    Abort trap: 6
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    It must be something specific about your platform or version of Tcl/Tk. I was able to insert ر without consequence.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      I am guessing that your version of Tkinter/TCL/Tk is too old to handle this. It runs fine on my machine also. Without the "coding utf-8" line the error is
      SyntaxError: Non-ASCII character '\xc3' in file
      which is how it should be handled, i.e. Python errors before it ever gets to the Tkinter interface.

      You can also try
      self.tTale.inse rt(END, u'blá'.encode(" utf-8"))
      and see if that works. If memory serves, versions of TCL 8.4 and up handle unicode. To print the version:
      Code:
      try:
          import Tkinter as tk     ## Python 2.x
      except ImportError:
          import tkinter as tk     ## Python 3.x
      print tk.TclVersion, tk.TkVersion

      Comment

      Working...