Home > Software engineering >  How to set up a waiting point for Tkinter
How to set up a waiting point for Tkinter

Time:11-20

I try to make a little gui with tkinter to improuve mental calculation. In my program, tkinter ask 10 question and the program should wait the good or bad answer before show the next question but I don't now how to wait properly

here is my code:

class PageAdd1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        self.id = controller.id

        lblTitle = tk.Label(self, text="Mode Addition Facile")  # titre de la fenêtre
        lblTitle.pack()

        btnBack = tk.Button(self, text="Revenir à la sélection de la difficulté",
                            command=lambda: controller.up_frame("PageAdd"))
        btnBack.pack()
        global score
        global flag
        global iteration
        score=0
        flag=False
        iteration=0

        def getEntry():
            global score
            global flag
            res=int(entryRes.get())
            if res == c:
                score= score  1
                flag=True
            print(score)

        btnValid = tk.Button(self, text="Valider", command=getEntry)
        btnValid.pack()
        lblQuestion = tk.Label(self, text="initiation")
        lblQuestion.pack()

        for i in range(9):
            flag=False
            a, b, c = addition(1)
            lstQuestion = ["Quelle est le résultat de l'oppération ", str(a), "   ", str(b), "?"]
            question = "".join(lstQuestion)
            lblQuestion = tk.Label(self, text=question)
            lblQuestion.pack()
            res=tk.IntVar()
            entryRes= tk.Entry(self, textvariable= res)
            entryRes.pack()
            print(i)

I try to put a flag with a while the flag is not true, and turn the flag true when I have the good answer but it doesn't work. I don't now how to do it... Thank you in advance

CodePudding user response:

I created a minimum case that fits your needs

import tkinter as tk

from tkinter import ttk


mainwindow = tk.Tk()
row = 0


def validation(widget,value,number1,number2):
   result = int(number1)   int(number2)
   if(value.isnumeric() and int(value) == result):
      addLayer(int(number1),int(number2) 1)
      mainwindow.nametowidget(widget)["state"] = "disabled"
   return True

   
def addLayer(number1,number2):
   global row
   lclFrame = tk.Frame(mainwindow)
   lclFrame.grid(row=row,column=0)
   if(row==10):
      tk.Label(lclFrame,text="You succesfully completed the test!").grid(row=0,column=0)
      return
   vcmd = lclFrame.register(validation)
   tk.Label(lclFrame,text=f"what is sum of {number1} and {number2}").grid(row=0,column=0)
   tk.Entry(lclFrame,validate="key",validatecommand=(vcmd,"%W","%P",number1,number2)).grid(row=1,column=0)
   row  =1

addLayer(2,3)

mainwindow.mainloop()

You can use Entry widgets validate feature. In example above, every key stroke made into entry widget sent into validation function. Only if validation function returns True then entry text would change so we always return True. In validatecommand= parameters, we add arguments which we want to send validating function. In validation function, we check if value given by user is true, we add another layer of question by calling addLayer function. However, if row number which means in this case number of questions is 10. We disable correctly entered entry boxes to prevent spamming layers.

This case only accepts true answers, however by editing validation function you can make anything you want with answers.

  • Related