I have written a simple program which creates maths multiplication problems. I wrote it as a CLI, however moved over to a GUI as some teachers were saying that the CLI was too small for pupils to see.
Everything works bar one thing. Once the 'start' button has been clicked I would like to update the listbox on the left with a new question after 5 seconds. In the CLI version I simply added sleep(5) which paused the program and then resumed, however the GUI halts the whole program and waits, then spits out all the questions.
If I remove the for loop the teacher can click the button 15 times, but this seems wasteful.
Here's my code:
from tkinter import *
from tkinter import messagebox
from time import sleep
from random import randint
questionList=[]
def main():
for i in range (15):
num1= randint(0,12)
num2 = randint(1,12)
question = ("Question",(i+1),")",num1,"X",num2)
listbox.insert(END, question)
#sleep(5)
tempArray = []
tempArray.append(num1)
tempArray.append(num2)
questionList.append(tempArray)
def answers ():
for i in range (len(questionList)):
ans = (questionList[i][0]*questionList[i][1])
listbox1.insert(END,ans)
root = Tk()
root.geometry("445x590+460+70")
root.title("Maths Machine")
label = Label(root, text="Maths Machine", font = ("Arial",16)).grid(row = 0, columnspan = 2)
startButton = Button(root, text = "Start", width = 15, command = main).grid(row = 1, column = 1,padx = 10, pady = 10)
label = Label(root, text = "Questions", font = ("Arial",12)).grid(row = 2, column = 0)
label = Label(root, text = "Answers", font = ("Arial",12)).grid(row = 2, column = 1)
listbox = Listbox(root, width = 25, height = 15,font = ("Arial",16))
listbox.grid(row = 3, column = 0)
listbox1 = Listbox(root, width = 10, height = 15,font = ("Arial",16))
listbox1.grid(row = 3, column = 1)
answerButton = Button(root, text ="Show answers", width = 15, command = answers).grid(row = 4, column = 1, padx =10, pady =10)
mainloop()