用pycharm做python的gui界面,利用entry.get()得到键盘输入的答案时,出现了
File "D:\pycharm\djangoProject3\multi_gui.py", line 121, in surecallback
result = int(entry01.get())
ValueError: invalid literal for int() with base 10: ''的报错
困惑:搞不懂自己哪里输入了编译不了的值,系统要报错,不过结果又正常……
import tkinter as tk
import random
import time
window = tk.Tk()
score = 0
count = 1
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01 = tk.StringVar()
mul01.set(str(a))
mul02 = tk.StringVar()
mul02.set(str(b))
count_title = tk.StringVar()
count_title.set('第1道题')
score_lab = tk.StringVar()
score_lab.set('0')
answer_lab = tk.StringVar()
gameover = tk.StringVar()
gameover.set("共测试10道乘法,每题10分,总分100分")
window.title("乘法口诀测试")
window.geometry('600x500')
window["background"] = "LightCyan"
title01 = tk.Label(window,
textvariable=gameover,
font=('华文行楷', 15),
bg='SpringGreen')
title01.place(x=120, y=30)
label01 = tk.Label(window,
textvariable=count_title,
font=('华文行楷', 20),
bg='hotpink') # 第N道题
label01.place(x=100, y=100)
title02 = tk.Label(window, text="请输入答案:", font=('华文行楷', 20),
bg='hotpink')
title02.place(x=80, y=250)
title03 = tk.Label(window,
textvariable=score_lab,
font=('华文行楷', 28),
bg='hotpink') # 分数lab
title03.place(x=340, y=400)
title04 = tk.Label(window, text="得分:", font=('华文行楷', 28), bg='hotpink') # 得分lab
title04.place(x=190, y=400)
title05 = tk.Label(window, textvariable=answer_lab, font=('华文行楷', 28), bg='hotpink') # 得分lab
title05.place(x=220, y=450)
entry01 = tk.Entry(window, textvariable=0, width=10, font=('华文行楷', 20), show=None) # 文本输入框
entry01.place(x=300, y=250)
label02 = tk.Label(window, textvariable=mul01, font=('华文行楷', 28),
bg='hotpink') # 被乘数lab
label02.place(x=180, y=175)
label03 = tk.Label(window, textvariable=mul02, font=('华文行楷', 28),
bg='hotpink') # 乘数lab
label03.place(x=340, y=175)
imgLabel = tk.Label(window, text='X', font=('黑体', 28),
bg='hotpink') # 乘号标签
imgLabel.place(x=263, y=170)
def surecallback():
global a, b, count
for count in range(1, 11):
product = a * b
result = int(entry01.get())
if result == product:
global score
score = score + 10
score_lab.set(str(score))
answer_lab.set("答对了")
else:
answer_lab.set("答错了")
entry01.delete(0, tk.END)
a = random.randint(1, 9)
b = random.randint(1, 9)
mul01.set(str(a))
mul02.set(str(b))
count = count + 1
c = '第' + str(count) + '道题'
count_title.set(c)
gameover.set('测试已结束!')
count_title.set('第1道题')
button01 = tk.Button(text='确认',
font=('华文行楷', 20),
width=8,
height=1,
command=surecallback)
button01.place(x=230, y=310)
window.mainloop()
surecallback函数里为什么要用for循环?
for count in range(1, 11):
这样只有第一次判断的时候有效,判断结束后,entry01.delete(0, tk.END) 就把输入框清空,但是没有退出for循环,所以 result = int(entry01.get()) 里是空字符串,无法转成整数