[Tutor] Floating point error in tkinter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] Floating point error in tkinter

Válas Péter
Hi,

I use a tkinter-based editor that ran properly earlier on English Windows XP. Now I use Hungarian Windows 7 with Python 2.7.2. You must know that in Hungary decimal fractions are marked with a decimal comma, not a dot (e.g. pi=3,1415...).

I suspect it somehow gets a Hungarian decimal from Windows and can't understand it. So when I page up and down with PgUp/PgDn buttons, it works well. When I use the wheel on mouse, it's OK, too. But when I try to use the vertical scrollbar of the window with mouse, error messages flood my screen and it won't work. Dou you think this is a Python bug? Shall I make a bugreport or just some config setting is missing?

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0016"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0033"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0049"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0066"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0082"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0098"
Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
    res = self.tk.call(self._w, 'yview', *args)
TclError: expected floating-point number but got "0,0098"

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Floating point error in tkinter

Steven D'Aprano-8
On Fri, Mar 09, 2012 at 02:09:23PM +0100, Válas Péter wrote:

> Hi,
>
> I use a tkinter-based editor that ran properly earlier on English Windows
> XP. Now I use Hungarian Windows 7 with Python 2.7.2. You must know that in
> Hungary decimal fractions are marked with a decimal comma, not a dot (e.g.
> pi=3,1415...).
>
> I suspect it somehow gets a Hungarian decimal from Windows and can't
> understand it. So when I page up and down with PgUp/PgDn buttons, it works
> well. When I use the wheel on mouse, it's OK, too. But when I try to use
> the vertical scrollbar of the window with mouse, error messages flood my
> screen and it won't work. Dou you think this is a Python bug?

What makes you think it is a Python bug? Is there something that makes
you expect that Tkinter should accept strings with commas instead of
floats?

The error message is pretty clear: tkinter is expecting a float, but
receiving a string instead.

TclError: expected floating-point number but got "0,0016"

Unless there is documentation that says that it will accept strings
with commas, this is not a Python bug. It may be a bug in the editor,
but not in the Python language.

By the way, it seems that a lot of other code has this same problem.
You might find that running this:

import locale
locale.setlocale(locale.LC_ALL, 'english_us')

just before the editor starts up might help:

http://forums.arcgis.com/threads/27427-arcpy-and-easygui-eventloop-conflict



--
Steven

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Floating point error in tkinter

Alan Gauld
In reply to this post by Válas Péter
On 09/03/12 13:09, Válas Péter wrote:

> flood my screen and it won't work. Dou you think this is a Python bug?

It's probably not a Python bug but a Tcl/Tk bug.
It would be worth asking about it on a Tcl/Tk forum - or maybe even the
Tkinter list.

>    File "c:\python27\lib\lib-tk\Tkinter.py", line 1444, in yview
>      res = self.tk.call(self._w, 'yview', *args)
> TclError: expected floating-point number but got "0,0033"

Note that this error comes from Tcl not Python.

Tcl/Tk often evaluates strings as a float but may well be expecting a
decimal period rather than a comma in the string. (It really just uses
the float as an encoding for two integers!)

Can you hard code the period in your code? Or trap the string and do a
replace() to convert it before it calls the Tkinter code?

On the other hand it could just be that it expects a literal float and
not a string representation of the same... With no sight of the original
Python code it's hard to say. But Tk uses string floats often enough
that I suspect that it is a Tcl/Tk issue.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Floating point error in tkinter

Válas Péter
In reply to this post by Steven D'Aprano-8
Sorry for sending it in private first, Steven drew my attention to it. This list has a strange setting, I got used to lists that send replies automatically to list, and did not notice the error.

2012. március 9. 18:53 Válas Péter írta, <[hidden email]>:
2012/3/9 Steven D'Aprano <[hidden email]>

What makes you think it is a Python bug? Is there something that makes
you expect that Tkinter should accept strings with commas instead of
floats?
Better to say: I think it is a Tkinter bug (unless we expect Windows to adapt itself to Python), and Tkinter is part of the Python dstribution AFAIK. As you may see, Tkinter is the only module to appear in the error message. We may say it is a Windows bug, but this is the case of the elephant and the mouse. :-)
 
The error message is pretty clear: tkinter is expecting a float, but
receiving a string instead.
No; it receives a float formatted by Hungarian standards as set in control panel of Windows, and does not recognize it as a float.
 

import locale
locale.setlocale(locale.LC_ALL, 'english_us')
That's what I definitely wouldn't like to do, because I do need Hungarian locale very much for everything else. Too big a price.

VP



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Floating point error in tkinter

Alan Gauld
On 10/03/12 19:40, Válas Péter wrote:

>     Better to say: I think it is a Tkinter bug (unless we expect Windows
>     to adapt itself to Python), and Tkinter is part of the Python

But it's really a Tcl bug because Tkinter is built on top of Tcl/Tk
and (mostly) cannot fix bugs that exist in the underlying framework.

>         The error message is pretty clear: tkinter is expecting a float, but
>         receiving a string instead.
>
>     No; it receives a float formatted by Hungarian standards as set in
>     control panel of Windows, and does not recognize it as a float.

No, it is receiving a string - note the quotation marks.
But Tcl is a strange language that interprets many variables
as strings and, especially in Tk, it is common to pass numerical
values as strings. (In early Tcl versions all variables were strings
interpreted by Tcl at runtime, one of the most common complaints
and a reason for its early lethargy. Nowadays Tcl uses native
types like most other languages and is no longer sloth like...
But some artifacts remain)


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor