Home > Blockchain >  Trying to make a GUI tkinter that generates a random number, takes the user's guess and compare
Trying to make a GUI tkinter that generates a random number, takes the user's guess and compare

Time:11-19

I'm new to python and right now I'm trying to make a GUI Tkinter that gives a random integer from 1-9 then I would take an entry from the user and his bet, if the user's guess is correct then his bet would be added to his money(money = 1000), and if wrong, it would subtract the bet from his money and all of it has to be done in one button. My problem is that with my code the first comparing would be correct but then after that it would always be false like it doesn't read my if-else statement anymore. And the variable money which has a value of 1000 always resets the second time I clicked the button, like for the first time, if the bet is 500 and the guess is correct, it would add but after that it would always reset, instead of the number being 2000 after the second guess, it would start again at 1500 like it doesn't change the original variable.

I don't know what should I do, please help.

import tkinter as tk
import random

window = tk.Tk()
window.title("Guessing Game")
window.geometry("600x600")
window.configure(bg="Orange")

num = random.randint(1, 9)


def my_guess():
    guess = int(txtGuess.get())
    bet = int(txtBet.get())
    money = 1000
    if guess == num:
        money = money   bet
        lblBet.config(text=money)
        lblNumber.config(text=random.randint(1, 9))
    else:
        money = money - bet
        lblBet.config(text=money)
        lblNumber.config(text=random.randint(1, 9))


lblNumber = tk.Label(window, text=num, fg="blue",  bg="Violet", font=("Bold", 25), width=5, 
height=2)
lblNumber.place(x=30, y=180)

lblRandom = tk.Label(window, text="Random", fg="indigo", bg="black", font=("Bold", 25), 
width=8, height=2)
lblRandom.place(x=10, y=20)

lblMoney = tk.Label(window, text="Money", fg="indigo", bg="black", font=("Bold", 25), width=8, 
height=2)
lblMoney.place(x=210, y=20)

lblBet = tk.Label(window, text="1000", fg="blue",  bg="Violet", font=("Arial", 27), width=8, 
height=2)
lblBet.place(x=210, y=180)

lblGuess = tk.Label(window, text="Guess", fg="indigo", bg="black", font=("Bold", 25), width=8, 
height=2)
lblGuess.place(x=410, y=20)

txtGuess = tk.Entry(window, text="", fg="blue", bg="Violet", font=("Arial", 25), width=8)
txtGuess.place(x=410, y=180)

txtBet = tk.Entry(window, text="",  fg="blue", bg="Violet", font=("Arial", 15), width=20)
txtBet.place(x=195, y=400)

btnBet = tk.Button(window, text="Bet", font=("Arial", 15), width=20)
btnBet.place(x=195, y=450)


window.mainloop()

CodePudding user response:

You forgot the basics. You need to add a command option to run the function when button is clicked -

btnBet = tk.Button(window, text="Bet", font=("Arial", 15), width=20,command=my_guess)

Note - You should not add () here when using command.
You can add it if you used lambda, for e.g - command=lambda:my_guess()

CodePudding user response:

The problem is that you set the money to 1000 inside the guess function, the money value should be extracted from the lblBet label. Also the num should also be extraded from the random label.

The updated guess function:

def my_guess():
    guess = int(txtGuess.get())
    bet = int(txtBet.get())
    money = int(lblBet["text"])
    num = int(lblNumber["text"])
    if guess == num:
        money = money   bet
        lblBet.config(text=money)
        lblNumber.config(text=random.randint(1, 9))
    else:
        money = money - bet
        lblBet.config(text=money)
        lblNumber.config(text=random.randint(1, 9))

You also need to add the guess function to the button.

btnBet = tk.Button(window, text="Bet", font=("Arial", 15), width=20, command=my_guess)
  • Related