TomSchimansky / CustomTkinter

A modern and customizable python UI-library based on Tkinter
MIT License
10.99k stars 1.04k forks source link

CTkEntry raises a TclError when using IntVar or DoubleVar #734

Open legopitstop opened 1 year ago

legopitstop commented 1 year ago

CTkEntry raises an error when you use IntVar or DoubleVar as a textvariable

customtkinter: 5.0.0 OS: Windows 10

Steps To Reproduce

  1. Run the below code in a python file.
  2. Enter any character that is not a number ex: "Hello, World"
  3. A TclError gets thrown every time you add or remove a character.

Observed Results

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"
Buldes commented 1 year ago

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")
legopitstop commented 1 year ago

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.

jishnuvinayak commented 10 months ago

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.