CustomTkinter
CustomTkinter copied to clipboard
CTkEntry raises a TclError when using IntVar or DoubleVar
CTkEntry raises an error when you use IntVar or DoubleVar as a textvariable
customtkinter: 5.0.0
OS: Windows 10
Steps To Reproduce
- Run the below code in a python file.
- Enter any character that is not a number ex: "Hello, World"
- A TclError gets thrown every time you add or remove a character.
Observed Results
- Only effects the IntVar and DoubleVar.
- Throws the error when you remove or add characters.
- Entering a valid value (integer for IntVar and float for DoubleVar) does not throw an error.
- If you remove
0from the entry box it will raise the error
Code:
from customtkinter import CTk, CTkEntry
import tkinter
root = CTk()
root.minsize(200,200)
var1 = tkinter.IntVar()
var2 = tkinter.IntVar()
entry1 = CTkEntry(root, textvariable=var1) # Raises an error
entry1.pack()
entry2 = tkinter.Entry(root, textvariable=var2) # Does not raise an error
entry2.pack()
root.mainloop()
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\1589l\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 570, in get
return self._tk.getint(value)
_tkinter.TclError: expected integer but got "hello, worl"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\1589l\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "C:\Users\1589l\AppData\Local\Programs\Python\Python310\lib\site-packages\customtkinter\windows\widgets\ctk_entry.py", line 116, in _textvariable_callback
if self._textvariable.get() == "":
File "C:\Users\1589l\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 572, in get
return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got "hello, worl"
It does'nt work, because you tried to put a string in a int variable.
So use tkinter.StringVar() except of tkinter.IntVar()
If you then want the int out of the string, use int()
example:
var = tkinter.StringVar()
var.set('hello, world')
try:
var_in_int = int(var)
except:
print("Can't convert string into int")
I'm aware that you should use StringVar for strings however this is showing the issue that If you enter or remove characters that are not an int or float it will raise an error.
The problem is the validation check that is happening each time the value in the widget gets modified, you already have an integer value in the entry widget and want to enter a new value. When you delete the value, the widget will throw the validation error even before entering the new value. It's a bit annoying, each time when you remove a value and enter a new value, you will get this validation error in your console.