Search code examples
pythontkintertkinter-entry

How to add functionality to my try again button in tkinter?


I have a written a code which generates two numbers and asks user to calculate its sum and input the result. Program works fine but what I want is to add a button which again asks user to input the result and verify that. The problem I am facing is that the entry is not being cleared and another window is opening below the original window with same entry.

import tkinter as tk
import random
window = tk.Tk()
content = tk.StringVar()
def startgame():
    number1 = random.randint(10, 51)
    number2 = random.randint(10, 51)
    sum_of_num = number1 + number2

    def checkresult():
        global content
        i = int(content.get())
        if i == sum_of_num:
            label = tk.Label(window, text = "Correct Result", fg = "blue").pack()
        else:
            label = tk.Label(window, text = "Incorrect result!! Try again.", fg = "red").pack()


    window.title("Number Guessing Game")
    label = tk.Label(window, text = "Let's begin the Quiz", fg = "blue").pack()
    label = tk.Label(window, text = "---------------------", fg = "blue").pack()
    label = tk.Label(window, text = "Given two numbers", fg = "blue").pack()
    label = tk.Label(window, text = number1, fg = "blue").pack()
    label = tk.Label(window, text = number2, fg = "blue").pack()
    label = tk.Label(window, text = "Add these two numbers and enter the result", fg = "blue").pack()

    sum_of_numbers = tk.Entry(window, textvariable = content)
    sum_of_numbers.pack()
    sum_of_numbers.focus_set()

    button_to_check_result = tk.Button(window, text="Check whether answer is correct", fg = "red", command = checkresult).pack()


startgame()    
button_to_try_again = tk.Button(window, text="Try Again !!", fg = "red", command = startgame).pack()
window.mainloop()

I expect the same window again with cleared inputs.


Solution

  • In order to clear the window, you need to pack all the labels into a single frame. For that, you need to first import ttk from tkinter:

    from tkinter import ttk
    fr = ttk.Frame()
    

    Now, all the master value for the variables need to be changed from 'window' to 'fr'. But as a simpler way, what I have done is to use the variable 'window' as the frame. So,

    root= tk.Tk()
    window = ttk.Frame(root)
    

    Now, to clear the frame, you can use any of the following commands:

    window.pack_forget()
    window.destroy()
    

    The final modified code is as follows:

    import tkinter as tk
    import random
    from tkinter import ttk
    root = tk.Tk()
    content = tk.StringVar()
    global window
    window = ttk.Frame(root)
    
    def startgame():
        global window
        window.pack_forget()
        window = ttk.Frame(root)
        number1 = random.randint(10, 51)
        number2 = random.randint(10, 51)
        sum_of_num = number1 + number2
    
        def checkresult():
            global content
            i = int(content.get())
            if i == sum_of_num:
                label = tk.Label(window, text = "Correct Result", fg = "blue").pack()
            else:
                label = tk.Label(window, text = "Incorrect result!! Try again.", fg = "red").pack()
    
    
        root.title("Number Guessing Game")
        label = tk.Label(window, text = "Let's begin the Quiz", fg = "blue").pack()
        label = tk.Label(window, text = "---------------------", fg = "blue").pack()
        label = tk.Label(window, text = "Given two numbers", fg = "blue").pack()
        label = tk.Label(window, text = number1, fg = "blue").pack()
        label = tk.Label(window, text = number2, fg = "blue").pack()
        label = tk.Label(window, text = "Add these two numbers and enter the result", fg = "blue").pack()
    
        sum_of_numbers = tk.Entry(window, textvariable = content)
        sum_of_numbers.pack()
        sum_of_numbers.focus_set()
    
        button_to_check_result = tk.Button(window, text="Check whether answer is correct", fg = "red", command = checkresult).pack()
        window.pack()
    
    startgame()    
    button_to_try_again = tk.Button(root, text="Try Again !!", fg = "red", command = startgame).pack()
    window.mainloop()