Моя программа - математический тест.Он принимает вопросы, выбирая их случайным образом из текстового файла (questiontest.txt).Я хочу, чтобы мой код генерировал тест более одного раза, и он делает это, за исключением отображения.Даже если фактические вопросы и требуемые ответы изменились, отображаемый вопрос не меняется с первого выбранного вопроса, поэтому новые вопросы не отображаются.
Это код:
from tkinter import *
import tkinter as tk
import random
title_font= ("Microsoft Jhenghei UI Light", 35)
class MathsApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand= True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.title("Maths Revision App")
self.geometry("800x500")
self.frames = {}
for F in (StartPage, q1, q2, q3, q4, q5, resultPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
#funtion that displays different pages
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.configure(bg ="white")
lbl = tk.Label(self, text="Welcome!", font=title_font, bg="white", fg="#004d99")
lbl.place(x=30, y=20)
rtbtn=tk.Button(self, text="Start", height=3, width=15, fg="white", bg="#004d99", command=lambda:[controller.show_frame(q1)])
rtbtn.place(x=350, y=200)
class q1(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Question 1", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=Text(self, height=5, width=70)
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[0][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=Entry(self)
ans.place(x=650, y=350,height=25)
nxt=Button(self, text="Next", height=3, width=15, fg="white", bg="#004d99", command=lambda:[checkAns(ans.get(), 0),controller.show_frame(q2)])
nxt.place(x=650, y=400)
class q2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Question 2", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=Text(self, height=5, width=70)
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[1][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=Entry(self)
ans.place(x=650, y=350,height=25)
nxt=Button(self, text="Next", height=3, width=15, fg="white", bg="#004d99", command=lambda:[checkAns(ans.get(), 1),controller.show_frame(q3)])
nxt.place(x=650, y=400)
class q3(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Question 3", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=Text(self, height=5, width=70)
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[2][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=Entry(self)
ans.place(x=650, y=350,height=25)
nxt=Button(self, text="Next", height=3, width=15, fg="white", bg="#004d99", command=lambda:[checkAns(ans.get(), 2),controller.show_frame(q4)])
nxt.place(x=650, y=400)
class q4(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Question 4", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=Text(self, height=5, width=70)
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[3][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=Entry(self)
ans.place(x=650, y=350,height=25)
nxt=Button(self, text="Next", height=3, width=15, fg="white", bg="#004d99", command=lambda:[checkAns(ans.get(), 3),controller.show_frame(q5)])
nxt.place(x=650, y=400)
class q5(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Question 5", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
txt=Text(self, height=5, width=70)
txt.config(state="normal")
txt.insert(tk.INSERT,qlist[4][0])
txt.config(state="disabled")
txt.place(x=35,y=125)
ans=Entry(self)
ans.place(x=650, y=350,height=25)
nxt=Button(self, text="End Test", height=3, width=15, fg="white", bg="#004d99", command=lambda:[checkAns(ans.get(), 4),resultReview(),controller.show_frame(resultPage)])
nxt.place(x=650, y=400)
class resultPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.configure(bg="white")
lbl=Label(self, text="Results", font=("Microsoft Jhenghei UI Light", 35), bg="white", fg="#004d99")
lbl.place(x=30, y=20)
nxt=Button(self, text="Restart", height=3, width=15, fg="white", bg="#004d99", command=lambda:[refresh(),controller.show_frame(StartPage)])
nxt.place(x=650, y=400)
qlist=0
def getQuestions():
global qlist
qlist=[0,0,0,0,0]
noschosen=[]
with open("questiontest.txt","r") as f:
lines=f.read().strip().split("\n")
i=0
while i<len(qlist):
chosen=False
n=random.randint(1,20)
while n in noschosen:
n=random.randint(1,20)
qline=2*n-1
noschosen.append(n)
qlist[i]=(lines[qline],lines[qline+1])
i=i+1
getQuestions()
results=[]
def checkAns(ans, questionAns):
global results
if ans == qlist[questionAns][1]:
results.append("right")
else:
results.append("wrong")
correct=0
def resultReview():
global correct
for index in range (0,len(results)):
if results[index]=="right":
correct=correct+1
print (results)
print ("You got", correct,"out of 5 right")
def refresh():
global qlist,results, correct
qlist=0
results=[]
correct=0
getQuestions()
print (qlist)
app = MathsApp()
app.mainloop()
TXT-файл содержит:
.
Question1
Answer1
Question2
Answer2
Question3
Answer3
Question4
Answer4
Question5
Answer5
Question6
Answer6
Question7
Answer7
Question8
Answer8
Question9
Answer9
Question10
Answer10
Question11
Answer11
Question12
Answer12
Question13
Answer13
Question14
Answer14
Question15
Answer15
Question16
Answer16
Question17
Answer17
Question18
Answer18
Question19
Answer19
Question20
Answer20
Ответом на вопросы является «AnswerX», X - номер вопроса в текстовом поле.Есть ли способ изменить текст, который отображается в текстовых полях?Или нет другого выхода, кроме как закрыть и сразу же снова открыть программу?Если так, то как бы я это сделал?