-2

I am trying to make a module that generates a tkinter window and then packs a question with a set of answers. My issue is getting the algorithm to produce the sets of question and answers one after the other. What I mean by this is that the algorithm currently only packs the answer sets and doesn't wait until after the player has answered to refresh and produce a new set. I have been thinking about this for ages and I am new to tkinter so I don't know what I am doing. My code is below:

This is my code for checking if an answer is correct or not

score = IntVar(window, 0)
score.set(0)

def check_answer(answer):
    global r
    global order
    if answer in order:
        global score
        s = score.get() + 1
        score.set(s)
        lbl.config(window, textvariable=score)
    else:
        lbl.config(text="Wrong")

This is my code that produces the answer sets and packs them into the window:

r = len(questions)
order= []
question = StringVar()

for i in range(r):

    question.set(questions[i])
    selected=tkinter.Label(window, textvariable=question.get())
    selected.config(textvariable=question.get())
    selected.pack()
    for j in answers[i]:
        if j != answers[i][0]:
            answer = tkinter.Button(window, text=j, command=lambda answer=j: check_answer(answer))
            answer.pack()
            order.append(i)
    lbl = tkinter.Label(window,textvariable=score.get())
    lbl.pack()
  • Maybe you can have a look at [sleep](https://docs.python.org/3/library/time.html#time.sleep) – ViG Feb 08 '18 at 10:44
  • I think the reason is how Tkinter runs its window. It runs the code in a loop continuously to keep the window active. Maybe if you try putting the event checking inside a function, it could work. – RPT Feb 08 '18 at 10:53
  • I think you should create a question frame class and call one after another using button commands like in [here](https://stackoverflow.com/a/7557028/7032856). – Nae Feb 08 '18 at 12:11
  • Welcome to [so]! Please provide a [mcve] to the problem you're having as opposed to some sections of your actual code. – Nae Feb 08 '18 at 12:50

1 Answers1

1

I'm not sure understanding what you want but i guess it is something like :

  • when the user start the programm -> Display one question and some anwsers
  • when the user clic on an anwser -> Display one other question and some other anwsers

So you must call the code that display the set of question inside your check_answer function.

e.g. :

def display_question(i):
    # your code to display question i
    # ...
    for j in answers[i]:
        # ...
        answer = tkinter.Button(window, text=j, command=lambda : check_answer(j, i))

def check_answer(answer, question):
    # your code for checking what you want and counting score
    # ...
    if answer in order:
        # ....
        display_question(question + 1)

# and at the beginind :
display_question(0)
cdrom
  • 591
  • 2
  • 16