This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: setting a locale that uses comma as decimal separator breaks tkinter.DoubleVar
Type: behavior Stage:
Components: Tkinter Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, epaine, serhiy.storchaka, thawn
Priority: normal Keywords:

Created on 2020-03-02 15:11 by thawn, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test_doublevar.py thawn, 2020-03-02 15:11 minimal code example
Messages (5)
msg363184 - (view) Author: Till Korten (thawn) * Date: 2020-03-02 15:11
This issue occurs when a locale is set that uses comma as decimal separator (e.g. locale.setlocale(locale.LC_NUMERIC, 'de_DE.utf8')).
I have a tkinter.Spinbox with increment=0.1 connected to a tkinter.DoubleVar.
When I change the value of the Spinbox using the arrow buttons and subsequently try to read out the variable with tkinter.DoubleVar.get(), my code throws the follwoing error:
_tkinter.TclError: expected floating-point number but got "0,1".

Here is a minimal code example:

-------------
import tkinter
import locale

locale.setlocale(locale.LC_NUMERIC, 'de_DE.utf8')


class TestDoubleVar():
    def __init__(self):
        root = tkinter.Tk()
        self.var = tkinter.DoubleVar()
        self.var.set(0.8)
        number = tkinter.Spinbox(
            root,
            from_=0, to=1, increment=0.1,
            textvariable=self.var,
            command=self.update,
            width=4
        )
        number.pack(side=tkinter.LEFT)
        root.mainloop()

    def update(self, *args):
        print(float(self.var.get()))


if __name__ == '__main__':
    TestDoubleVar()

-------

Actual result: the code throws an error

Expected result: the code should print the values of the DoubleVar even with a locale set that uses comma as the decimal separator.

n.b. the problem also occurs with tkinter.Scale
msg363371 - (view) Author: Till Korten (thawn) * Date: 2020-03-04 18:18
I just found the following code in lines 314-320 of [tkinter/ttk.py](https://github.com/python/cpython/blob/master/Lib/tkinter/ttk.py), which are clearly not localization-aware:
```
def _to_number(x):
    if isinstance(x, str):
        if '.' in x:
            x = float(x)
        else:
            x = int(x)
    return x
```

I'll keep looking for similar stuff and add a pull request once I think I have nailed down the issue
I'll look for something similar in
msg371394 - (view) Author: E. Paine (epaine) * Date: 2020-06-12 16:35
Is this a problem exclusive to tkinter? I ran the following code, and it appears to be an issue with the builtin 'float' method:

>>> import locale
>>> locale.setlocale(locale.LC_NUMERIC, 'de_DE.utf8')
'de_DE.utf8'
>>> float("1,2")
ValueError: could not convert string to float: '1,2'

I have somehow managed to corrupt my locales so I would appreciate if you could try on your machine as well (is it just me?!).
msg400005 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-21 00:40
I wrote a longer explanation but BPO and Chrome ate it.

The issue is that DoubleVar is not locale aware. I don't know what should be the actual fix but the following may be a useful workaround (It works on my system):

import tkinter
import locale
import sys

locale.setlocale(locale.LC_NUMERIC, 'de_DE')
class Var(tkinter.DoubleVar):
    def get(self):
        return locale.atof(self._tk.globalgetvar(self._name))

class TestDoubleVar():
    def __init__(self):
        root = tkinter.Tk()
        self.var = Var()
        self.var.set(0.8)
        number = tkinter.Spinbox(
            root,
            from_=0, to=1, increment=0.1,
            textvariable=self.var,
            command=self.update,
            width=4
        )
        number.pack(side=tkinter.LEFT)
        root.mainloop()
    def update(self, *args):
        print(self.var.get())
if __name__ == '__main__':
    TestDoubleVar()
msg400023 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-21 08:08
See also issue12558 which exposes the other side of the same issue.

Las time I looked at it, it was inherently a Tcl issue. In some places it uses locale-dependent formatting of floating point numbers, but locale-unaware parsing, or vice versa.
History
Date User Action Args
2022-04-11 14:59:27adminsetgithub: 84008
2021-08-21 08:08:14serhiy.storchakasetmessages: + msg400023
2021-08-21 00:40:04andrei.avksetnosy: + andrei.avk
messages: + msg400005
2020-06-12 16:35:37epainesetnosy: + epaine
messages: + msg371394
2020-03-07 09:49:43terry.reedysetnosy: + serhiy.storchaka
2020-03-04 18:18:50thawnsetmessages: + msg363371
2020-03-02 15:11:30thawncreate