samedi 16 janvier 2021

restart random number guessing game

So far, I have created a game in python to guess a random number. It works fine, but I want to create a reset button that will allow the user to guess one another time but different number than the first time. This is my code so far

        import tkinter as tk
        import random

        randombroj=random.randint(0, 10)

        def check():
           result=int(glvnes.get())
           if result==randombroj:
              gldugme.grid_forget()
              glvnes.grid_forget()
    
              bravo=tk.Label(root, text="Congratulations!!")
              bravo.config(width=40)
              bravo.grid(pady=0, row=1)

              def restart():
                 randombroj=random.randint(0, 10)
                 bravo.destroy()
                 weset.destroy()
                 gldugme.grid(pady=20, row=2)
                 glvnes.grid(pady=0, row=1)

                 weset=tk.Button(root, text="Restart", font=("arial italic", 18), bg='#6BDF63', command=restart)
                 weset.config(width=13, heigh=2)
                 weset.grid(pady=20, row=2)

        root=tk.Tk()
        root.title("Guess The  Secret Number")
        root.geometry('450x250')
        root.config(bg="#F2E06B")

        glmeni=tk.Label(root, text="Can  you gess the secret number?", bg='#8CCEC0', font=("arial italic", 19))
        glmeni.config(heigh=2)
        glmeni.grid(pady=30, padx=28, row=0)

        glvnes=tk.Entry(root, text="Enter number here:")
        glvnes.config(width=40)
        glvnes.grid(pady=0, row=1)

        gldugme=tk.Button(root, text="Check", font=("arial italic", 18), bg='#6BDF63', command=check)
        gldugme.config(width=13, heigh=2)
        gldugme.grid(pady=20, row=2)

        root.mainloop()

Wnen you press the check button, if it is correct it will display congratulations, and than the check button is replaced with reset button. When I click it, the entry widget and the check button are reopening, but the random value doesn't change. It stays as it was the first time. How do I fix it?




Aucun commentaire:

Enregistrer un commentaire