|
楼主 |
发表于 2020-3-15 10:41:27
|
显示全部楼层
本帖最后由 lucky邪神 于 2020-3-15 10:44 编辑
import tkinter
#import os
import random
class workitem():
def __init__(self):
#可以考虑将主界面关闭
#读取数据库
self.file1=open('alldata.txt','r')
self.str1=self.file1.readlines()
self.file1.close()
self.itemnum = len(self.str1)
#取第一个题目
num=random.randint(0,self.itemnum-1)
#for num in range(itemnum)
self.questionitem = self.str1[num].split(":::")[0]
self.answeritem = self.str1[num].split(":::")[1]
self.worktk=tkinter.Tk(className=' Study Item')
self.worktk.geometry("1000x600+200+50")
self.Label1=tkinter.Label(self.worktk,text='题目内容:',font=(15))
self.Label1.grid(row=0,pady=10,column=0,padx=30,columnspan=1)
self.text1=tkinter.Text(self.worktk,bg='green',width=60,height=10,font=(15))
self.text1.grid(row=1,column=1,rowspan=1,columnspan=4)
self.text1.insert('0.0',self.questionitem)
self.Label2=tkinter.Label(self.worktk,text='作答区:',font=(15))
self.Label2.grid(row=2,pady=10,column=0,padx=30,columnspan=1)
self.text2=tkinter.Text(self.worktk,bg='white',width=60,height=10,font=15)
self.text2.grid(row=3,column=1,rowspan=1,columnspan=4)
self.submitbutton=tkinter.Button(self.worktk,text='提交答案',font=("仿宋",15),command=self.submitresult)
self.submitbutton.grid(padx=5,row=4,column=2)
self.nextsubmitbutton=tkinter.Button(self.worktk,text='下一个题',font=("仿宋",15),command=self.nextitem)
self.nextsubmitbutton.grid(padx=5,row=4,column=3)
pass
"""
def getitem(self):
self.file1=open('alldata.txt','r')
str1=self.file1.readlines()
self.file1.close()
itemnum = len(str1)
print(itemnum)
print(str1)
print(type(str1[0]))
num=random.randint(itemnum)
#for num in range(itemnum)
questionitem = str1[num].split(":")[0]
answeritem = str1[num].split(":")[1]
return (questionitem,answeritem)
"""
def submitresult(self):
#对答案做对比判断,弹出对话框给出结果
if self.text2.get('0.0','end') == self.answeritem:
ResultText = "正确,你真棒!"
else:
ResultText = "很遗憾!错了!"
self.text2.delete('0.0','end')
self.text2.insert('0.0',self.answeritem)
self.judgement(ResultText)
pass
def judgement(self,dxt):
jud = tkinter.Tk(className = "结果判定")
jud.geometry("250x100+550+255")
lab=tkinter.Label(jud,text=dxt,font=15)
lab.grid(row=1,padx=40,pady=10)
button=tkinter.Button(jud,text="OK",font=15,command=jud.destroy)
button.grid(row=2,padx=42,pady=11)
def nextitem(self):
#从题库中随机抽取一个题
self.text1.delete('0.0','end')
self.text2.delete('0.0','end')
num=random.randint(0,self.itemnum-1)
self.questionitem = self.str1[num].split(":::")[0]
self.answeritem = self.str1[num].split(":::")[1]
self.text1.insert('0.0',self.questionitem)
pass
class additem():
def __init__(self):
self.addtk=tkinter.Tk(className=' Add Item')
self.addtk.geometry("1000x600+200+50")
addtk=self.addtk
self.Label1=tkinter.Label(self.addtk,text='题目内容:',font=(15))
self.Label1.grid(row=0,pady=10,column=0,padx=30,columnspan=1)
self.text1=tkinter.Text(self.addtk,bg='green',width=60,height=10,font=(15))
self.text1.grid(row=1,column=1,rowspan=1,columnspan=4)
self.Label2=tkinter.Label(self.addtk,text='答案解析:',font=(15))
self.Label2.grid(row=2,pady=10,column=0,padx=30,columnspan=1)
self.text2=tkinter.Text(self.addtk,bg='white',width=60,height=10,font=15)
self.text2.grid(row=3,column=1,rowspan=1,columnspan=4)
self.submitbutton=tkinter.Button(self.addtk,text='提交题库',font=("仿宋",15),command=self.submititem)
self.submitbutton.grid(padx=5,row=4,column=2)
self.nextsubmitbutton=tkinter.Button(self.addtk,text='下一个题',font=("仿宋",15),command=self.nextsubmititem)
self.nextsubmitbutton.grid(padx=5,row=4,column=3)
def submititem(self):
#将题目和答案解析存储,考虑这两个元素组合成{题目:答案}的字典做为另一个字典的value,序号作为键值key
file1=open('alldata.txt','a+')
submitdict={} #考虑从库中读取
#输入为空的情况后续在更新
textitem = self.text1.get('0.0','end')
answeritem = self.text2.get('0.0','end')
submitdict[textitem] = answeritem #新输入的题目和答案
file1.writelines(textitem[:-1])
file1.write(':::')
file1.writelines(answeritem[:-1])
file1.write('\n')
file1.close()
#print(submitdict)
#print(textitem.get('0.0','end'))
pass
def nextsubmititem(self):
#将写好的题存储到题库,清除前面两个文本框中的值
#self.submititem() #暂时考虑写完一个题目后已经提交题库
self.text1.delete('0.0','end')
self.text2.delete('0.0','end')
pass
welcometk = tkinter.Tk(className='Welcome')
welcometk.geometry("500x220+400+270")
welcomelabel= tkinter.Label(welcometk,text="欢迎使用儿童练习册",bg="blue",font=("仿宋",16),width=40,height=3)
welcomelabel.grid(row=0,rowspan=2,column=1,columnspan=3,padx=30,pady=10)
addbutton =tkinter.Button(welcometk,text='增加新题',font=('宋体',15),bg='yellow',width=15,height=2,command=additem)
addbutton.grid(row=2,padx=50,pady=20,column=0,columnspan=2)
#addbutton.pack(padx=200,pady=100)
workbutton = tkinter.Button(welcometk,text='做题',font=('宋体',15),bg='green',width=15,height=2,command=workitem)
workbutton.grid(row=2,column=2,columnspan=2)
#workbutton.pack(padx=100,pady=150)
welcometk.mainloop()
|
|