I recently learned a bit of python. Im trying to make a number guessing game on a frame widget. Random Number button is pressed to generate a random number. The Enter button then takes the input from the Entry Box and compares to the random number generated. To test it the program a label that shows the random number generated. When i input the same number as the one generated it says its not equal. The only time it says its equal is when i input 0(zero). I also have some lines that tell me what type the values are just to make sure I'm comparing the same types.
from random import randint
from tkinter import *
root = Tk()
root.title('Guess Number Final')
root.geometry('500x500')
directionFrame = Frame(root, width = 200, height = 200, highlightbackground = 'red',
highlightthickness = 3)
directionFrame.grid(row = 0, column = 0, padx = 20, pady = 20, ipadx = 20, ipady = 20)
randomNum = IntVar()
newguessInt = IntVar()
def randomize():
randomNum = randint(0,10)
randomStr.set(randomNum)
def enterGuess():
newguessInt = int(number_input.get())
newRandom = randomNum.get()
if (newguessInt == newRandom):
guess_var.set('Your guess is right.')
else:
guess_var.set('Your guess is wrong.')
guess_str.set(type(newguessInt))
random_str.set(type(newRandom))
##### Random Button #####
randomBtn = Button(directionFrame, text = 'Random Number', command = randomize)
randomBtn.pack()
##### Random Variable #####
randomStr = StringVar()
randomStr.set('Number')
##### Guess Variable #####
guess_var = StringVar()
guess_var.set('Your guess is...')
##### typeGuessVariable #####
guess_str = StringVar()
guess_str.set('Type')
##### randomNumVariable #####
random_str = StringVar()
random_str.set('Type')
##### Random Label #####
randomLbl = Label(directionFrame, textvariable = randomStr)
randomLbl.pack()
##### Number Entry Box #####
number_input = Entry(directionFrame)
number_input.pack()
##### Enter Button #####
enterBtn = Button(directionFrame, text = 'Enter Guess', command = enterGuess)
enterBtn.pack()
##### Your Guess Label #####
yourGuessLbl = Label(directionFrame, textvariable = guess_var)
yourGuessLbl.pack()
##### GuessInt Label ######
guessIntLbl = Label(directionFrame, textvariable = guess_str)
guessIntLbl.pack()
##### Random Label ######
randomNumLbl = Label(directionFrame, textvariable = random_str)
randomNumLbl.pack()
mainloop()
CodePudding user response:
You have declared many tkinter variables that confuse you to use a wrong ones for comparison.
Actually using only two tkinter variables is enough:
randomNum
for random generated numberguessNum
for user guess number
from random import randint
from tkinter import *
root = Tk()
root.title('Guess Number Final')
root.geometry('500x500')
directionFrame = Frame(root, width=200, height=200, highlightbackground='red', highlightthickness=3)
directionFrame.grid(row=0, column=0, padx=20, pady=20, ipadx=20, ipady=20)
randomNum = StringVar(value='Number')
guessNum = StringVar(value='')
def randomize():
randomNum.set(randint(0,10))
def enterGuess():
ok = randomNum.get() == guessNum.get().strip()
resultLabel['text'] = f'Your guess is {"right" if ok else "wrong"}.'
##### Random Button #####
randomBtn = Button(directionFrame, text='Random Number', command=randomize)
randomBtn.pack()
##### Random Label #####
randomLbl = Label(directionFrame, textvariable=randomNum)
randomLbl.pack()
##### Number Entry Box #####
number_input = Entry(directionFrame, textvariable=guessNum)
number_input.pack()
##### Enter Button #####
enterBtn = Button(directionFrame, text='Enter Guess', command=enterGuess)
enterBtn.pack()
##### Result Label #####
resultLabel = Label(directionFrame, text='Your guess is ...')
resultLabel.pack()
mainloop()
CodePudding user response:
This line:
randomNum = randint(0,10)
creates a new local variable randomNum
and assigns it to a regular Python int
value. The original randomNum
in the outer scope is still a default IntVar
with a value of zero. What I think you want to do is:
randomNum.set(randint(0, 10))
i.e. set the IntVar
you created to point to a new random int.