Python Forum
[Tkinter] Implementing number of guesses left in guessing game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Implementing number of guesses left in guessing game
#1
I am building a guessing game where the user has to guess what the capital of a country is by looking at the image of the country's flag. For this I have made a GUI using the tkinter module.

I have added a function answer() which indicates if the user input is right or wrong. It works fine when the user enters the correct answer but does not get proper results if an incorrect answer is entered.

Could someone advise where I am going wrong and a possible solution for this.

from tkinter import *

# Initialization
root = Tk()

# Placing the GUI window on the top left corner of the user's computer
root.geometry('+0+0')
# Setting the title of the game
root.title("Flag Guessing Game")
# Setting the dimensions of the GUI window
root.geometry("300x400")

# Defining the heading of the game
lbl_heading = Label(root, text = "Welcome to this fun game!")
lbl_heading.pack(pady = 10)
lbl_heading.config(font=("Helvetica", 12, 'bold'))

# Inserting an image
image = PhotoImage(file="image2l.png")
setting = Label(root, image = image)
setting.pack()

# Define a new window when Rules is clicked
def create_window1():
    window1 = Toplevel(root)
    window1.title("Rules for this game")
    window1.geometry("600x150")
    tb1 = Label(window1, text = "The rules for this game are given below:")
    tb1.config(font=("Helvetica", 8, 'bold'))
    tb1.pack()
    tb2 = Label(window1, text = "1. Once you click on START, you will enter the game premises.")
    tb2.pack()
    tb3 = Label(window1, text = "2. Look at the image of the given flag.")
    tb3.pack()
    tb4 = Label(window1, text = "3. If you know which country the flag belongs to, please enter it in the text box provided.")
    tb4.pack()
    tb5 = Label(window1, text = "4. You will proceed to the next question only if you answer the current question correctly.")
    tb5.pack()
    tb6 = Label(window1, text = "5. If your answer is wrong, you will get threee chances after which you will need to restart the game.")
    tb6.pack()
    window1.mainloop()

# Create a button to read the rules of the game
B1 = Button(root, text="Rules", command=create_window1, width = 5)
B1.config(font=("Helvetica", 10, 'bold'))
B1.pack(pady = 15) 

# Define a new window when START is clicked
def create_window2():
    global e1
    global window2
    window2 = Toplevel(root)
    window2.title("Game Premises")
    window2.geometry("400x600")
    lbl_heading2 = Label(window2, text = "Guess the capital of this country:")
    lbl_heading2.pack(pady = 10)
    lbl_heading2.config(font=("Helvetica", 12, 'bold'))
    
    # Insert flag image
    image2 = PhotoImage(file="USFlag.png")
    setting2 = Label(window2, image = image2)
    setting2.pack()

    # User input button
    Label(window2, text="Enter the capital").pack(pady = 10)
    e1 = Entry(window2)
    e1.pack(pady = 10)
    user_input = e1.get()

    # Create a submit button
    B2 = Button(window2, text="Submit", command=answer, width = 10)
    B2.config(font=("Helvetica", 10, 'bold'))
    B2.pack(pady = 15)

    # Create a hint button 
    H = Button(window2, text="Hint", command = lambda root = window2: labeltext(window2), width = 5)
    H.config(font=("Helvetica", 10, 'bold'))
    H.pack(pady = 10)

    # Next button
    nextb = Button(window2, text="Next", command=second, width = 5)
    nextb.config(font=("Helvetica", 10, 'bold'))
    nextb.pack(pady = 10)

    quitb = Button(window2, text="Exit", command=window2.destroy, width = 5)
    quitb.config(font=("Helvetica", 10, 'bold'))
    quitb.pack(pady = 10)
    
    window2.mainloop()

def secondpage():
    global e2
    global window3
    window3 = Toplevel(root)
    window3.title("Game Premises")
    window3.geometry("400x600")
    lbl_heading2 = Label(window3, text = "Guess the capital of this country:")
    lbl_heading2.pack(pady = 10)
    lbl_heading2.config(font=("Helvetica", 12, 'bold'))
    
    # Insert flag image
    image2 = PhotoImage(file="USFlag.png")
    setting2 = Label(window3, image = image2)
    setting2.pack()
    setting2.image = image2

    # User input button
    Label(window3, text="Enter the capital").pack(pady = 10)
    e2 = Entry(window3)
    e2.pack(pady = 10)
    user_input2 = e2.get()
    
    # Create a submit button
    B2 = Button(window3, text="Submit", command=answer2, width = 10)
    B2.config(font=("Helvetica", 10, 'bold'))
    B2.pack(pady = 15)

    # Create a hint button 
    H = Button(window3, text="Hint", command = lambda root = window3: labeltext2(window3), width = 5)
    H.config(font=("Helvetica", 10, 'bold'))
    H.pack(pady = 10)

    # Previous button
    prevb = Button(window3, text="Previous", command=previouspage, width = 8)
    prevb.config(font=("Helvetica", 10, 'bold'))
    prevb.pack(pady = 10)

    quitb = Button(window3, text="Quit", command=window3.destroy, width = 5)
    quitb.config(font=("Helvetica", 10, 'bold'))
    quitb.pack(pady = 10)

    window3.mainloop()

def labeltext(create_window2):
    global labelt
    labelt = Label(window2, text = "Name of country: USA")
    labelt.pack()

def labeltext2(create_window2):
    global labelt
    labelt = Label(window3, text = "Name of country: India")
    labelt.pack()

def second():
    window2.destroy()
    secondpage()

def previouspage():
    window3.destroy()
    create_window2()

def answer():
    guessed = False
    tries = 2
    while not guessed and tries != 0:
        user_input = e1.get()
        if user_input == "Washington":
            Label(window2, text="Correct answer!").pack(pady = 10)
        else:
            Label(window2, text="Incorrect answer. Please try again").pack()
            Label(window2, text="You have " + str(tries) + " tries left").pack()
            tries -= 1
            if tries == 0:
                Label(window2, text="All tries have been exhausted. Please restart and try again").pack()
        
def answer2():
    user_input2 = e2.get()
    guesses_remaining = 3
    counter = 0
    if user_input2 == "New Delhi":
        Label(window3, text="Correct answer!").pack(pady = 10)
    else:
        Label(window3, text="Incorrect answer. Please try again").pack(pady = 10)
    
# Create a button to start the game
B3 = Button(root, text="START", command=create_window2, width = 10)
B3.config(font=("Helvetica", 14, 'bold'))
B3.pack()

# Exit button
exit_bmain = Button(root, text="Exit", command=root.destroy, width = 5)
exit_bmain.config(font=("Helvetica", 10, 'bold'))
exit_bmain.pack(pady = 10)

root.mainloop()
Reply


Messages In This Thread
Implementing number of guesses left in guessing game - by Alienator - May-27-2021, 10:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Implementing tk.after zukochew 5 3,674 Aug-10-2018, 12:47 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020