Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tkinter.TclError: bad screen distance "200.0" #571

Open
snoo-snoo opened this issue Oct 30, 2022 · 16 comments · Fixed by JiaPai12138/CustomTkinter#4 · May be fixed by #2468
Open

tkinter.TclError: bad screen distance "200.0" #571

snoo-snoo opened this issue Oct 30, 2022 · 16 comments · Fixed by JiaPai12138/CustomTkinter#4 · May be fixed by #2468

Comments

@snoo-snoo
Copy link

Python 3.10
Windows 11

When i init a CtkFrame like this:

 def __init__(self, parent, data):
        super().__init__(parent)

i always get

_tkinter.TclError: bad screen distance "200.0"

The error is located at
ctk_frame.py line 41:

self.canvas = CTkCanvas(master=self, highlightthickness=0, width=self.apply_widget_scaling(self._current_width), height=self.apply_widget_scaling(self._current_height))

when changing to:


        self.canvas = CTkCanvas(master=self,
                                highlightthickness=0,
                                width=int(self.apply_widget_scaling(self._current_width)),
                                height=int(self.apply_widget_scaling(self._current_height)))

it works, but then next "TclError: bad screen distance" is located in ctk_label.py, line 47.

Tried it on several computers, but error is always the same.

Any idea what this might cause? looks it somehow receives a float instead of an int, but width and height are default values.

@TheFallen-Cat
Copy link

Can you send a minimal reproducible example?

P.S. Looking at the widgets, might it be a problem in the base_widget_class.py with the return value of the apply_widget_scaling function?

@snoo-snoo
Copy link
Author

snoo-snoo commented Oct 31, 2022

Yeah, i got the problem, here is a minimal reproducible example:

import tkinter
import customtkinter
import locale

locale.setlocale(locale.LC_ALL, '')

customtkinter.set_appearance_mode("dark")  # Modes: "System" (standard), "Dark", "Light"
customtkinter.set_default_color_theme("blue")  # Themes: "blue" (standard), "green", "dark-blue"

app = customtkinter.CTk()
app.geometry("400x580")
app.title("CustomTkinter simple_example.py")


frame_1 = customtkinter.CTkFrame(master=app)
frame_1.pack(pady=20, padx=60, fill="both", expand=True)

label_1 = customtkinter.CTkLabel(master=frame_1, justify=tkinter.LEFT)
label_1.pack(pady=12, padx=10)


app.mainloop()

The problem is, when using the locale package, then the integer values get converted to floats.
But have no idea how to fix it

@TheFallen-Cat
Copy link

TheFallen-Cat commented Nov 1, 2022

@snoo-snoo I tried your code, and it works without any problem on my machine.
image

@snoo-snoo
Copy link
Author

@TheFallen-Cat try changing your OS language to German. Then you should be able to replicate the error. sorry, forgot about that.

@TheFallen-Cat
Copy link

Ah okay, lemme try that in a bit.

@TheFallen-Cat
Copy link

TheFallen-Cat commented Nov 4, 2022

@snoo-snoo I did. And if i leave the locale.setlocale(locale.LC_ALL, ' ') like this it works, but if i put locale.setlocale(locale.LC_ALL, 'de_du') it throws the error. So, maybe try running your code with empty quotation marks as locale will auto-detect the language of the system, no need to put the langugae code(de_du) specifically.

@jicart
Copy link

jicart commented Jan 18, 2023

I'm having the same problem. I've turned off the locale change as a workaround, but I'm wondering if there might be a fix for it.
This is the code I've used to reproduce the issue:

import locale
import customtkinter as ctk

root = ctk.CTk()
label = ctk.CTkLabel(root, text="some text")

locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
label2 = ctk.CTkLabel(root, text="some text") # throws _tkinter.TclError: bad screen distance "0.0"

@jicart
Copy link

jicart commented Feb 9, 2023

It would be nice to have a better error message for this bug.
I'm kind of ashamed to admit it, but I've spent about 20 min trying to debug this same error again. Yeah, I know. Pretty stupid.
The error message is way too cryptic and I did not remember it.
I only realized it was this issue again when I looked into one particular import that was changing locale.

@Wolf-SO
Copy link

Wolf-SO commented Feb 10, 2023

After reducing the example given by @snoo-snoo even more to

import customtkinter
import locale

locale.setlocale(locale.LC_ALL, '')
app = customtkinter.CTk()
customtkinter.CTkFrame(master=app)

I can still reproduce the issue on a German Win10 (customtkinter-5.1.2, Python 3.11.1):

C:\Test>python ctk-issue-571.py
Traceback (most recent call last):
  File "C:\Test\ctk-issue-571.py", line 7, in <module>
    customtkinter.CTkFrame(master=app)
  File "C:\Python311\Lib\site-packages\customtkinter\windows\widgets\ctk_frame.py", line 56, in __init__
    self._canvas = CTkCanvas(master=self,
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python311\Lib\site-packages\customtkinter\windows\widgets\core_rendering\ctk_canvas.py", line 31, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Python311\Lib\tkinter\__init__.py", line 2744, in __init__
    Widget.__init__(self, master, 'canvas', cnf, kw)
  File "C:\Python311\Lib\tkinter\__init__.py", line 2628, in __init__
    self.tk.call(
_tkinter.TclError: bad screen distance "200.0"

@TomSchimansky
Copy link
Owner

I will have a look.

@Heribert17
Copy link

Heribert17 commented Mar 13, 2023

I have the same problem. It seems to be a bug in tkinter when converting the float to string for tk.
You can test it with this piece of code. When commenting the call to locale it works, with the locale call you get the errormessage. When you change the width and height to integers it also works with the locale call.

from tkinter import
import locale

locale.setlocale(locale.LC_ALL, "")
# create a GUI window widget
window=Tk()
# create an inner window
cv =Canvas(window,width=500.0,height=500.0)
cv.pack()
# create a rectangle
cv.create_rectangle(50,20,200,200,fill="red")

window.mainloop()

I found this discussion in the Python forum about float in tkinter and German locale
[https://github.com/python/cpython/issues/56767]

@Heribert17
Copy link

During migration of one of my projects to customertkinter i steped into the same problem again. I'm on Winbdows 10, Python 3.11.6 and newest custometkinter. The problem occurs when using a locale (for eample locale.setlocale(locale.LC_ALL, "DE-de")).
A solution for that problem is to modify the _apply_widget_scaling and _reverse_widget_scaling in scaling_base_class.py to return integer and not float. After that change the programm excutes without error. I also tested it with the complex_example.py by adding the locale setting.
This ist the change in scaling_base_class.py

    # changed float -> int in return (and Union)
    def _apply_widget_scaling(self, value: Union[int, float]) -> Union[int]:
        assert self.__scaling_type == "widget"
        return int(value * self.__widget_scaling)
    # # changed float -> int in return (and Union)
    def _reverse_widget_scaling(self, value: Union[int, float]) -> Union[int]:
        assert self.__scaling_type == "widget"
        return int(value / self.__widget_scaling)

@Heribert17
Copy link

I found that you need the float value, otherwise scrollbars arn't working. I changed the code above to:

    def _apply_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value * self.__widget_scaling
        else:
            return int(value * self.__widget_scaling)

    def _reverse_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value / self.__widget_scaling
        else:
            return int(value / self.__widget_scaling)

@OddUnicorn
Copy link

I found that you need the float value, otherwise scrollbars arn't working. I changed the code above to:

    def _apply_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value * self.__widget_scaling
        else:
            return int(value * self.__widget_scaling)

    def _reverse_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value / self.__widget_scaling
        else:
            return int(value / self.__widget_scaling)

Had no problems since writing my program over half a year ago. But today the error "bad screen distance" suddenly appeared, even after disabling setlocale. Your solution fixed it, thanks.

@Jan-Weigang
Copy link

Can confirm this. Just ran into the same issue on german locale. Changed the above things in scaling_base_class.py. No errors afterwards.

@Bug38
Copy link

Bug38 commented Feb 14, 2024

I found that you need the float value, otherwise scrollbars arn't working. I changed the code above to:

    def _apply_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value * self.__widget_scaling
        else:
            return int(value * self.__widget_scaling)

    def _reverse_widget_scaling(self, value: Union[int, float]) -> Union[float, int]:
        assert self.__scaling_type == "widget"
        if isinstance(value, float):
            return value / self.__widget_scaling
        else:
            return int(value / self.__widget_scaling)

I just ran into the same issue, your solution fixed it thanks a lot!

ElSaico added a commit to ElSaico/CustomTkinter that referenced this issue Jun 13, 2024
Prevents triggering a bug on Tk canvas, where float parameters use locally-aware parsing and thus might expect a different decimal point character

Fixes TomSchimansky#571
@ElSaico ElSaico linked a pull request Jun 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants