Python tkinter input command cannot convert to int

Trying to get used to tkinter gui, but I had a problem setting the input field. I wanted to make a simple number guessing program, using Entry to enter an integer and a button to send fortune-telling. I get an int to int conversion error when I use int (GuessBox.get ()) and I'm not sure what to do.

ValueError: invalid literal for int () with base 10: ''

from tkinter import * import random def makeAGuess(): guess = int(GuessBox.get()) print(guess) if guess == Answer: print("you got it!") return False elif guess > Answer: print("Too High, try again") return True else : print("Too low, try again") return True Answer = random.randint(1, 100) main = Tk() label = Label(main, text = "Guess a number") label.pack() GuessBox = Entry(master = main) GuessBox.pack() submitGuess = Button(master = main, text = "Submit Guess", command = makeAGuess()) submitGuess.pack() main.mainloop() 
+4
source share
1 answer

You need to pass the function as an object, not call it.

 submitGuess = Button( master = main, text = "Submit Guess", command = makeAGuess ) 

Otherwise, makeAGuess is called when the Button is created, but no arguments are passed. With this change, your code works fine for me.

+4
source

Source: https://habr.com/ru/post/1483454/


All Articles