Question

This is my first question here on Stackoverflow. I have been learning Python for some time now, but I don't really get what is wrong about my code. Everytime I run my program, I get this error:

Traceback (most recent call last):
File "D:\Python\guess the number gui.py", line 63, in <module>
app = Application(root)
File "D:\Python\guess the number gui.py", line 14, in __init__
self.create_widgets()
File "D:\Python\guess the number gui.py", line 37, in create_widgets
command = self.check_number()
File "D:\Python\guess the number gui.py", line 44, in check_number
guess = int(self.guess.get())
ValueError: invalid literal for int() with base 10: ''

This is the code:

from tkinter import *
import random

class Application(Frame):
    """ GUI application for the "Guess my number" game"""

    def __init__(self, master):
        """Initialize Frame. """
        super(Application, self).__init__(master)
        self.the_number = random.randint(1,100)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """Create widgets to get the number and show if it's correct"""
        # create instruction label
        Label(self,
              text = "I'm thinking of a number between 1-100. Can you guess it?"
              ).grid(row = 0, column = 0, columnspan = 2, sticky = W)

        # create label and text entry for the guess
        Label(self,
              text = "Guess: "
              ).grid(row = 1, column = 0, sticky = W)
        self.guess = Entry(self)
        self.guess.grid(row = 1, column = 1, sticky = W)

        # create a text field
        self.correct_txt = Text(self, width = 40, height = 5, wrap = WORD)
        self.correct_txt.grid(row = 3, column = 0, columnspan = 2)        

        # create a submit button
        Button(self,
               text = "Click to submit",
               command = self.check_number()
               ).grid(row = 2, column = 0, sticky = W)


    def check_number(self):

        # get the guess of the user
        guess = int(self.guess.get())

        # see if the guess is correct or wrong
        if guess == self.the_number:
            text = ("Congratulations, you guessed it! And it only took you", tries, "tries!")
            self.correct_txt.delete(0.0, END)
            self.correct_txt.insert(0.0, text)

        elif guess < self.the_number:
            text = "Higher..."
            self.correct_txt.delete(0.0, END)
            self.correct_txt.insert(0.0, text)

        elif guess > self.the_number:
            text = "Lower..."
            self.correct_txt.delete(0.0, END)
            self.correct_txt.insert(0.0, text)

# main
root = Tk()
root.title("Guess the number")
app = Application(root)
root.mainloop()

Can somebody tell me what's wrong? Thanks in advance!

Était-ce utile?

La solution

In Button, command should refer to the name of the command, not a call to the command. As you have it now, self.check_number() is being called when the button is created. Try this:

Button(self,
       text = "Click to submit",
       command = self.check_number
       ).grid(row = 2, column = 0, sticky = W)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top