ValueError: invalid literal for int() with base 10: '' python 3.4 tkinter
ValueError: invalid literal for int() with base 10: '' python 3.4 tkinter
我正在调用我的函数 mTime 以从 4 个输入框获取值,以检查时间,如果有值,我的代码可以正常工作,但如果输入框为空,就像“”,我的意思是用户删除输入框中的默认值“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: ''
已编辑
新代码
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()
但不工作
您正在将 textvariable
设置为 IntVar
。使用 StringVar
.
我很久以前就解决了这个问题,但从来没有把答案放在这里,反正我用try和exempt来获取和设置那些输入框的值
这里是代码
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
我正在调用我的函数 mTime 以从 4 个输入框获取值,以检查时间,如果有值,我的代码可以正常工作,但如果输入框为空,就像“”,我的意思是用户删除输入框中的默认值“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: ''
已编辑
新代码
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()
但不工作
您正在将 textvariable
设置为 IntVar
。使用 StringVar
.
我很久以前就解决了这个问题,但从来没有把答案放在这里,反正我用try和exempt来获取和设置那些输入框的值
这里是代码
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