0

I am trying to make a row of Entry widgets that add numeric values up and input the total into the left-most column. I am able to make the code function, but it is throwing an exception anytime I try and use the get() method on a variable class. Here is an example:

from tkinter import *

root = Tk()

ent1 = IntVar()
ent2 = IntVar()
ent3 = IntVar()


def callback(*args):
    ent1.set(ent2.get() + ent3.get())


frame = Frame(root)
frame.pack()

entry1 = Entry(frame, width=5, textvariable=ent1)
entry2 = Entry(frame, width=5, textvariable=ent2)
entry3 = Entry(frame, width=5, textvariable=ent3)

entry1.pack(side=LEFT)
entry2.pack(side=LEFT)
entry3.pack(side=LEFT)

ent1.trace_add('write', callback)
ent2.trace_add('write', callback)
ent3.trace_add('write', callback)

root.mainloop()

and here is the exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Paul Desktop\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 508, in get
    return self._tk.getint(value)
_tkinter.TclError: expected integer but got ""

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Paul Desktop\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:/Users/Paul Desktop/PycharmProjects/Pathfinder Character Sheet/EntryTest.py", line 15, in callback
    ent1.set(ent2.get() + ent3.get())
  File "C:\Users\Paul Desktop\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 510, in get
    return int(self._tk.getdouble(value))
_tkinter.TclError: expected floating-point number but got ""

I have no clue why this is happening and I could use some assistance in what to do!

Streeter
  • 23
  • 5
  • Well, when you backspace everything on an entry box then the value is `""` which then tries to save to `IntVar()` and as we know it cannot have a string value either 0 or an integer. – Saad Jun 19 '20 at 21:08
  • What would be the best method of making sure that never happens? – Streeter Jun 19 '20 at 22:29
  • To never let the entry box have either `""` or *any other value than an integer value*. – Saad Jun 19 '20 at 23:04

1 Answers1

1

Like being said in my comment, when you backspace everything on an entry box then the value is "" which then tries to save to IntVar() and as we know an IntVar() cannot have a string value, it can either be 0 or any other integer. So to fix this we first need to accept only the integer values from the user. See this answer to understand how to do that.

I've changed your code and made it more optimized, by restricting the input to integers only and, checking if any Entry widget is empty or not We can avoid all those errors.

Complete code:

from tkinter import *

def callback(text, wid):
    if str.isdigit(text) or not text:
        text = text if text else 0
        if wid == str(entry3):
            n = entry2.get() if entry2.get() else 0
        if wid == str(entry2):
            n = entry3.get() if entry3.get() else 0
        ent1.set(int(n) + int(text))
        return True
    return False

root = Tk()

ent1 = IntVar()

vcmd = (root.register(callback), '%P', '%W')
frame = Frame(root)
frame.pack()

entry1 = Entry(frame, width=5, textvariable=ent1)
entry2 = Entry(frame, width=5, validate='all', validatecommand=vcmd)
entry3 = Entry(frame, width=5, validate='all', validatecommand=vcmd)

entry1.pack(side=LEFT)
entry2.pack(side=LEFT)
entry3.pack(side=LEFT)

root.mainloop()
Saad
  • 3,340
  • 2
  • 10
  • 32