1

I am trying to set a text-box or EntryBox to 0 if this entry box is empty I can do it if the entrybox's textvariable is a String, but I cant if is a DoubleVar or an IntVar

here is my code

mGui = Tk()
mGui.title("GUI")
mGui.geometry('1250x650+10+10')

def mCheck():
  if len(name1.get()) == 0:
    name1.set('noidea')
    mGui.update()
  if (len(name2.get()) == 0):
    name2.set('nofkidea')
    mGui.update()
  if (value1.get()== Empty or (len(str(value2.get()))=0) or (len(str(float(value2.get()))) == 0)):
    name2.set(10)
    mGui.update()
  if (value2.get()== None or (len(str(value2.get()))=0) or (len(str(float(value2.get()))) == 0)):
    name2.set(99999)
    mGui.update()

value1 = DoubleVar()
value2 = DoubleVar()
name1 = StringVar()
name2 = StringVar()
mButtonLoad = Button(mGui, text = "check", command = mCheck, fg = 'Red').place(x=550,y=140)
tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=200,y=80)
vText2 = Entry(mGui, textvariable = value2).place(x=200,y=100)

mGui.after(1, mGui.update)
mGui.mainloop()

I am getting this error when I Try to set the DoubleVar textboxes to 0 if these ones are empty

ValueError: could not convert string to float: 
1
  • Maybe this is a problem: this (len(str(value2.get()))=0) should be (len(str(value2.get()))==0). Also what is Empty? Is it some variable or what?
    – Marcin
    Commented Feb 5, 2015 at 5:28

1 Answer 1

1

Anyway, I fixed your code, so it works as it supposed if I understand you correctly. The main part was to change your if statement to try and except statement. the reason is that DoubleVar will through ValueError exception if it cant get the double value. For example, when the Entry is empty or is a string. Also your mButtonLoad is always None, so I fixed that as well. Finally, you dont need to check length of a string to know if its empty. Its enough to use not to check it.

from tkinter import *


mGui = Tk()
mGui.title("GUI")
mGui.geometry('1250x650+10+10')

def mCheck():

  if not name1.get():
    name1.set('noidea')
    mGui.update()

  if not name2.get():
    name2.set('nofkidea')
    mGui.update()


  try:
      print(value1.get())
  except ValueError as ve:
      name1.set(10)
      mGui.update()
      print(ve)

  try:
      print(value2.get())
  except ValueError as ve:
      name2.set(9999)
      mGui.update()
      print(ve)      


value1 = DoubleVar()
value2 = DoubleVar()

name1 = StringVar()
name2 = StringVar()

mButtonLoad = Button(mGui, text = "check", command = mCheck, fg = 'Red')
mButtonLoad.place(x=550,y=140)

tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=200,y=80)
vText2 = Entry(mGui, textvariable = value2).place(x=200,y=100)

mGui.after(1, mGui.update)
mGui.mainloop()
1
  • perfect, thanks men, I was struggling with this for a long time, I am new with python Commented Feb 5, 2015 at 23:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.