TK中 Tk.String() 不能实时传递的
各位大佬:我用TK界面写了个,随机出数学题,然后可以回答后自动判断结果的小程序。现在遇到问题,前面出题 排版都实现了,但是我再entry中回答后,好像结果不能被判断到。
问题是,我在 entry的 textvariable属性中加了变量。看上去 好像 判断还是空值。 实在没办法,真的是新手卡了我半个月,请大佬帮忙给看看。
# TK界面随机产生数学题
import openpyxl
import random
import tkinter as tk
from tkinter.filedialog import askdirectory
main = tk.Tk()
main.title ("随机出数学题")
group = tk.LabelFrame (main ,text ='选择出题类型',padx =5 ,pady =5)
group.grid(row=1,column=0)
vv= tk.IntVar()
r1 = tk.Radiobutton(group, text="加减法", variable=vv, value=1).grid(row=2,column=0)
r2 = tk.Radiobutton(group, text="乘法", variable=vv, value=2).grid(row=2,column=1)
r3 = tk.Radiobutton(group, text="整除法", variable=vv, value=3).grid(row=2,column=2)
v1 = tk.StringVar()
g2 = tk.LabelFrame (main ,text ='选择最小值' ,padx =5 ,pady =5)
g2.grid(row=3,column=0)
e1 = tk.Entry(g2,textvariable=v1).grid(row=3,column=1)
v2 = tk.StringVar()
g3 = tk.LabelFrame (main ,text ='选择最大值' ,padx =5 ,pady =5)
g3.grid(row=4,column=0)
e2 = tk.Entry(g3,textvariable=v2).grid(row=4,column=1)
lista = []#存储题目
listb =[] #存储答案
listll =[]#存储题目
liste= [] #存储entry名字
ans = [] #存储entry结果
ansf=[]
listl =[] #存储结果label名字
fed= [] #存储结果对应标签的textvariable值
def fun():
main1 =tk.Tk() #生产新的界面
main1.title ('数学计算题')
s =int(v1.get())
a =int( v2.get())
qty =1
for i in range (0,50):
ans1 ='a'+str(i)
ee = "e" + str(i)
ll = "l" + str(i)
lll = "ll"+str(i)
fed1 ='fed'+str(i)
liste.append(ee)
listl.append(ll)
listll.append(lll)
ans.append(ans1)
fed.append(fed1)
print (ans)
if vv.get() == 1 :#50道随机加减法
while qty <=50:
qty+=1
c=int(random.randint(s,a))
f=int(random.randint(s,a))
jz=random.randint(0,1)
if jz>=0.5:
test = ("%d. %d+%d = "%(qty-1,c,f))
res = c+f
else:
if c>= f:
test = ("%d. %d-%d = "%(qty-1,c,f))
res =c-f
else:
test = ("%d. %d-%d = "%(qty-1,f,c))
res = f-c
lista.append(test)
listb.append(res)
com3= tk.Button(main1,text = '提交结果',command = check)
com3.grid(row = 0,column = 2)
for qq in range (0,50):
ans =tk.StringVar()
listll = tk.LabelFrame(main1, text = lista,font=("微软雅黑", 13))
listll.grid(row = qq//5+1,column= qq%5)
liste = tk.Entry (listll,textvariable= ans).pack()
main1.mainloop()
def check():
fb=[]
for aas in range (0,50):
ans11 =ans.get()
print(ans11)
if ans11 ==listb:
fed='正确'
else:
fed='错误'
listl = tk.Label(listll,text=fed).pack()
print (fed)
com2 =tk.Button( main ,text ='出题',width=10,command =fun)
com2.grid (row =0 ,column =0)
main.mainloop() import openpyxl
import random
import tkinter as tk
from tkinter.filedialog import askdirectory
main = tk.Tk()
main.title ("随机出数学题")
group = tk.LabelFrame (main ,text ='选择出题类型',padx =5 ,pady =5)
group.grid(row=1,column=0)
vv= tk.IntVar()
r1 = tk.Radiobutton(group, text="加减法", variable=vv, value=1).grid(row=2,column=0)
r2 = tk.Radiobutton(group, text="乘法", variable=vv, value=2).grid(row=2,column=1)
r3 = tk.Radiobutton(group, text="整除法", variable=vv, value=3).grid(row=2,column=2)
v1 = tk.StringVar()
g2 = tk.LabelFrame (main ,text ='选择最小值' ,padx =5 ,pady =5)
g2.grid(row=3,column=0)
e1 = tk.Entry(g2,textvariable=v1).grid(row=3,column=1)
v2 = tk.StringVar()
g3 = tk.LabelFrame (main ,text ='选择最大值' ,padx =5 ,pady =5)
g3.grid(row=4,column=0)
e2 = tk.Entry(g3,textvariable=v2).grid(row=4,column=1)
lista = []#存储题目
listb =[] #存储答案
listll =[]#存储题目
liste= [] #存储entry名字
ans = [] #存储entry结果
ansf=[]
listl =[] #存储结果label名字
fed= [] #存储结果对应标签的textvariable值
def fun():
main1 = tk.Toplevel() #生产新的界面
sb = tk.Scrollbar(main1)
main1.title('随机数学题')
s =int(v1.get())
a =int( v2.get())
qty =1
for i in range (0,50):
ans1 ='a'+str(i)
ee = "e" + str(i)
ll = "l" + str(i)
lll = "ll"+str(i)
fed1 ='fed'+str(i)
liste.append(ee)
listl.append(ll)
listll.append(lll)
ans.append(ans1)
fed.append(fed1)
print (ans)
if vv.get() == 1 :#50道随机加减法
while qty <=50:
qty+=1
c=int(random.randint(s,a))
f=int(random.randint(s,a))
jz=random.randint(0,1)
if jz>=0.5:
test = ("%d. %d+%d = "%(qty-1,c,f))
res = c+f
else:
if c>= f:
test = ("%d. %d-%d = "%(qty-1,c,f))
res =c-f
else:
test = ("%d. %d-%d = "%(qty-1,f,c))
res = f-c
lista.append(test)
listb.append(res)
com3= tk.Button(main1,text = '提交结果',command = check)
com3.grid(row = 0,column = 2)
for qq in range (0,50):
ans =tk.StringVar()
listll = tk.LabelFrame(main1, text = lista,font=("微软雅黑", 13))
listll.grid(row = qq//5+1,column= qq%5)
liste = tk.Entry (listll,textvariable= ans).pack()
main1.mainloop()
def check():
fb=[]
for aas in range (0,50):
ans11 =ans.get()
if ans11 == '':
ans11 = 0
print(ans11)
if int(ans11) ==int(listb):
fed='正确'
else:
fed='错误'
listl = tk.Label(listll,text=fed).pack()
print (fed)
com2 =tk.Button( main ,text ='出题',width=10,command =fun)
com2.grid (row =0 ,column =0)
main.mainloop()
页:
[1]