ValueError:基数为10的int()的无效文字:'' python 3.4 tkinter

时间:2015-01-21 02:51:02

标签: python tkinter box tkinter-entry

我正在调用我的函数mTime来获取4个Entrybox中的值,以检查时间,如果有值我的代码可以正常工作但是如果输入框是空的,就像' ',我的意思是用户删除了输入框中的默认值,即' 0'。并将其留空我想指定一个值为' 0'如果发生这种情况

但它不起作用!

我的代码

def mTime():
  if (len(str(Days_T.get())) == 0):
    dd = Days_T.set(0)
  else:
    dd = Days_T.get()
  if (len(str(Hours_T.get())) == 0):
    hh = Hours_T.set(0)
  else:
    hh = Hours_T.get()
  if (len(str(Minutes_T.get())) == 0):
    mm = Minutes_T.set(1)
  else:
    mm = Minutes_T.get()
  if (len(str(Seconds_T.get())) == 0):
    ss = Seconds_T.set(0)
  else:
    ss = Seconds_T.get()
  d = dd
  h = hh
  m = mm
  s = ss
  print (dd,hh,mm,ss)

Days_T = IntVar()
Hours_T = IntVar()
Minutes_T = IntVar()
Seconds_T = IntVar()

TDays = Entry(mGui, textvariable = Days_T, width = 4, justify = CENTER).place(x=520,y=260)
THours = Entry(mGui, textvariable = Hours_T, width = 4, justify = CENTER).place(x=550,y=260)
TMinutes = Entry(mGui, textvariable = Minutes_T, width = 4, justify = CENTER).place(x=580,y=260)
TSeconds = Entry(mGui, textvariable = Seconds_T, width = 4, justify = CENTER).place(x=610,y=260)

mTime()

我收到此错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\me\Desktop\Project\ManualTab.py", line 908, in mTime
    if (len(str(Minutes_T.get())) == 0):
  File "C:\Python34\lib\tkinter\__init__.py", line 358, in get
    return getint(self._tk.globalgetvar(self._name))
ValueError: invalid literal for int() with base 10: ''

EDITED

新代码

  ddays = StringVar()
  ddays.set(Days_T.get())
  hhours = StringVar()
  hhours.set(Hours_T.get())
  mminutes = StringVar()
  mminutes.set(Minutes_T.get())
  sseconds = StringVar()
  sseconds.set(Seconds_T.get())
  if (len(ddays) == 0):
    dd = Days_T.set(2)
  else:
    dd = Days_T.get()
  if (len(hhours) == 0):
    hh = Hours_T.set(0)
  else:
    hh = Hours_T.get()
  if (len(mminutes) == 0):
    mm = Minutes_T.set(0)
  else:
    mm = Minutes_T.get()
  if (len(sseconds) == 0):
    ss = Seconds_T.set(0)
  else:
    ss = Seconds_T.get()  
  d = dd
  h = hh
  m = mm
  s = ss
  print (dd,hh,mm,ss)

Days_T = IntVar()
Hours_T = IntVar()
Minutes_T = IntVar()
Seconds_T = IntVar()

TDays = Entry(mGui, textvariable = Days_T, width = 4, justify = CENTER).place(x=520,y=260)
THours = Entry(mGui, textvariable = Hours_T, width = 4, justify = CENTER).place(x=550,y=260)
TMinutes = Entry(mGui, textvariable = Minutes_T, width = 4, justify = CENTER).place(x=580,y=260)
TSeconds = Entry(mGui, textvariable = Seconds_T, width = 4, justify = CENTER).place(x=610,y=260)

mTime()

但不工作

2 个答案:

答案 0 :(得分:2)

您将textvariable设置为IntVar。使用StringVar

答案 1 :(得分:0)

我很久以前就解决了这个问题,但从来没有把答案放在这里,无论如何我用过试试并免除了为这些输入框设置值

这里是代码

    try:
        Days_T.get()
    except ValueError:
        Days_T.set(0)
        mGui.update()
    try:
        Hours_T.get()
    except ValueError:
        Hours_T.set(0)
        mGui.update()
    try:
        Minutes_T.get()
    except ValueError:
        Minutes_T.set(0)
        mGui.update()
    try:
        Seconds_T.get()
    except ValueError:
        Seconds_T.set(0)
        mGui.update()
    d = Days_T.get()
    h = Hours_T.get()
    m = Minutes_T.get()
    s = Seconds_T.get()

然后我不需要担心输入框是否为空,如果发生则只分配0