Error: bad screen distance "140.0"
So I'm doing weather app using open weather api and when I'm trying to use ctk widgets it's giving me this error.
Traceback (most recent call last):
File "C:\Users\Janek\PycharmProjects\applications\main.py", line 62, in <module>
search_button = ctk.CTkButton(window, text='Search', command=search)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Janek\PycharmProjects\applications\venv\Lib\site-packages\customtkinter\windows\widgets\ctk_button.py", line 95, in __init__
self._canvas = CTkCanvas(master=self,
^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Janek\PycharmProjects\applications\venv\Lib\site-packages\customtkinter\windows\widgets\core_rendering\ctk_canvas.py", line 31, in __init__
super().__init__(*args, **kwargs)
File "C:\Users\Janek\PycharmProjects\applications\venv\Lib\site-packages\ttkbootstrap\style.py", line 5169, in __init__wrapper
func(self, *args, **kwargs)
File "C:\Users\Janek\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2744, in __init__
Widget.__init__(self, master, 'canvas', cnf, kw)
File "C:\Users\Janek\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2628, in __init__
self.tk.call(
_tkinter.TclError: bad screen distance "140.0"
My code: `import requests import tkinter as tk from tkinter import ttk import math from tkinter import messagebox from PIL import Image, ImageTk import ttkbootstrap import customtkinter as ctk
def get_weather(city): API_KEY = '862957c16730a84ba992b581fadd84e4' BASE_URL = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={API_KEY}"
result = requests.get(BASE_URL)
if result.status_code == 404:
messagebox.showerror("Error", "City not found")
return None
weather = result.json()
temperature = weather['main']['temp'] - 273.15
city = weather['name']
sky = weather['weather'][0]['main']
icon_id = weather['weather'][0]['icon']
# importing image
icon_url = f"http://openweathermap.org/img/wn/{icon_id}@2x.png"
return city, temperature, sky, icon_url
def search(): city = entry.get() result = get_weather(city)
(city, temperature, sky, icon_url) = result
image = Image.open(requests.get(icon_url, stream=True).raw)
icon = ImageTk.PhotoImage(image)
icon_label.configure(image=icon)
icon_label.image = icon
round_temp = round(temperature, 1)
city_name_label.configure(text=city)
temperature_label.configure(text=f"Temperature: {round_temp}°C")
appearance_label.configure(text=sky)
main
window = ttkbootstrap.Window(themename="vapor") window.geometry('800x600') window.title('Cloudly')
Entry
entry = tk.Entry(window, font=('Arial', 22)) entry.pack(pady=5)
Button
search_button = ttk.Button(window, text='Search', command=search) search_button.pack(pady=5)
city_name_label = tk.Label(window, text="", font=('Arial', 20)) city_name_label.pack(pady=10)
temperature_label = tk.Label(window, text="", font=('Arial', 20)) temperature_label.pack(pady=10)
icon_label = tk.Label(window) icon_label.pack(pady=10)
appearance_label = tk.Label(window, text="", font=('Arial', 20)) appearance_label.pack(pady=10)
run
window.mainloop()
`
What's the version your using?
Im using 5.2.0 customtkinter version
So i thing ctk has some problem with ttkbootstrap, when i type import ttkbootstrap it instantly gives me that error no matter what im doing
@anrunt - If you're unable to import ttkbootstrap, that is not an issue with customtkinter. They are completely unrelated. Perhaps check you have installed ttkbootstrap?
Looking at the error message, perhaps this is an error with ttkbootstrap?
@dishb well, for now I'm not able to check it but im pretty sure that i have installed ttkbootstrap because i can import it in other project without any problems. I will check it when I will be back home from vacation 🙂
@anrunt - Weird, but still, if you're getting an ImportError or something related in the line of code where you have the import statement, that isn't due to CustomTkinter. import statements are native to Python, and ImportErrors are only raised by Python.
@dishb oh okay, thank you so much for explanation 🫡
Hey friends, I stumbled upon exactly the same error. I'm using the example program from the readme.
The 140.0 that is displayed there is related to the width of the button (ctk_button.py:23 width: int = 140). This width is then transformed on line 95 to a float: with the self._apply_widget_scaling method. For reasons unknown to me, the float is not acceptable. If I change the method to:
def _apply_widget_scaling(self, value: Union[int, float]) -> Union[float]:
assert self.__scaling_type == "widget"
return int(value * self.__widget_scaling)
the example works fine in my case. I use python 3.11.6 on Windows 64 with customtkinter==5.2.2