473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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-----
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from Tkinter import *
  4. import tkMessageBox
  5. import sys
  6.  
  7. class App(object):
  8.     def __init__(self,parent):
  9.  
  10. # Frame
  11.         fSize = 10
  12.         self.f = Frame(parent,width=fSize,height=fSize)
  13.         self.f.pack(padx=15,pady=15)
  14.  
  15.         self.tTale = Text(self.f, height=15, width=30, bg="grey")
  16.         self.tTale.pack()
  17.         self.tTale.insert(END, u'blá')
  18.  
  19. #exit button
  20.         Button(text="quit", command=quit) .pack(side=BOTTOM)
  21.  
  22. root = Tk()
  23. root.title('Test')
  24. root.call('encoding', 'system', 'utf-8')
  25. app = App(root)
  26.  
  27. root.mainloop()
  28.  
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] -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds
2014-02-03 23:20:03.525 Python[82405:1107] (
0 CoreFoundation 0x00007fff8f59741c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8e1b4e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5972cc +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8f47ebcb -[__NSCFString characterAtIndex:] + 91
4 Tk 0x00000001083b9f5d TkpInitKeymapInfo + 825
5 Tk 0x00000001083bfecb Tk_MacOSXSetupTkNotifier + 861
6 Tcl 0x000000010829bccd Tcl_DoOneEvent + 311
7 _tkinter.so 0x00000001082194a3 init_tkinter + 4450
8 Python 0x0000000107f3414d PyEval_EvalFrameEx + 8080
9 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
10 Python 0x0000000107f388c8 _PyEval_SliceIndex + 929
11 Python 0x0000000107f354d4 PyEval_EvalFrameEx + 13079
12 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
13 Python 0x0000000107f31a24 PyEval_EvalCode + 54
14 Python 0x0000000107f50c2c PyParser_ASTFromFile + 306
15 Python 0x0000000107f50cd3 PyRun_FileExFlags + 137
16 Python 0x0000000107f50821 PyRun_SimpleFileExFlags + 718
17 Python 0x0000000107f61363 Py_Main + 2995
18 libdyld.dylib 0x00007fff8be895fd start + 1
)
2014-02-03 23:20:03.526 Python[82405:1107] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFConstantString characterAtIndex:]: Range or index out of bounds'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8f59741c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff8e1b4e75 objc_exception_throw + 43
2 CoreFoundation 0x00007fff8f5972cc +[NSException raise:format:] + 204
3 CoreFoundation 0x00007fff8f47ebcb -[__NSCFString characterAtIndex:] + 91
4 Tk 0x00000001083b9f5d TkpInitKeymapInfo + 825
5 Tk 0x00000001083bfecb Tk_MacOSXSetupTkNotifier + 861
6 Tcl 0x000000010829bccd Tcl_DoOneEvent + 311
7 _tkinter.so 0x00000001082194a3 init_tkinter + 4450
8 Python 0x0000000107f3414d PyEval_EvalFrameEx + 8080
9 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
10 Python 0x0000000107f388c8 _PyEval_SliceIndex + 929
11 Python 0x0000000107f354d4 PyEval_EvalFrameEx + 13079
12 Python 0x0000000107f32093 PyEval_EvalCodeEx + 1641
13 Python 0x0000000107f31a24 PyEval_EvalCode + 54
14 Python 0x0000000107f50c2c PyParser_ASTFromFile + 306
15 Python 0x0000000107f50cd3 PyRun_FileExFlags + 137
16 Python 0x0000000107f50821 PyRun_SimpleFileExFlags + 718
17 Python 0x0000000107f61363 Py_Main + 2995
18 libdyld.dylib 0x00007fff8be895fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Abort trap: 6
Feb 3 '14 #1
2 5273
bvdet
2,851 Expert Mod 2GB
It must be something specific about your platform or version of Tcl/Tk. I was able to insert ر without consequence.
Feb 4 '14 #2
dwblas
626 Expert 512MB
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.insert(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:
Expand|Select|Wrap|Line Numbers
  1. try:
  2.     import Tkinter as tk     ## Python 2.x
  3. except ImportError:
  4.     import tkinter as tk     ## Python 3.x
  5. print tk.TclVersion, tk.TkVersion 
Feb 4 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Edo van der Zouwen | last post by:
I have the following problem. On a website there's a (simple) feedback form. This is used also by Polish visitors who (of course) type Polish text using special characters. However, when I...
2
by: Srinath Avadhanula | last post by:
Hello, Sorry to be bringing up what seems to be a somewhat beaten up topic... This is what I wanted to do: Create a _simple_ text editor widget which supports VI(M) style keybindings but...
5
by: Otto Krüse | last post by:
Hi everyone, I'm building a GUI in which I want, amongst other things, for people to fill in there postal code. The postal codes of my country (Holland) are in this format: 1234 AB So for the...
2
by: Harlin Seritt | last post by:
I am working on a Find Text dialog box. Once you find a string in a Text widget, how do you at least move the cursor to that index (position)? Even better how can one 'select' the string one finds?...
0
by: [Yosi] | last post by:
How can I change the selection text back ground color (ritchText). I successfully change the text color by : this.MessageRichTextBox.SelectionColor = Color.Red How can I change also the Back...
1
by: Suresh Tri | last post by:
Hi, I was trying to overload concat operator ||(text,text) such a way that it behaves like Oracle. i.e. I want 'abc' || null to return 'abc' instead of null. I know that it is not the expected ...
2
by: Johann Robette | last post by:
Hi, I'm trying to call the array_to_string function like this : SELECT array_to_string(array, '~^~') --> it comes directly from the doc. I get this error msg : ERROR: parser: parse error at...
4
by: David Garamond | last post by:
What is "text + text" supposed to do right now? It doesn't seem very useful to me. What about making "text + text" as an equivalent for "text || text"? Most strongly-typed programming languages do...
2
by: Stefan Mueller | last post by:
Today I display a placeholder '.' on my webpage with <h3 id = "MyPlaceholder">.</h3> Later in my JavaScript I change the placeholder '.' to something else (e.g. 'My new text')...
2
by: manojkowshik | last post by:
Hi Guys, I'm new to Python. And I'm preparing a GUI in Python and I need to add scroll bar to the Text box. And I'm using 'Tkinter GRID' manager for this. Attached is the image of text box what I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.