user2038409
user2038409

Reputation: 13

Why don't I get the value of self.entry when I press my "Enter" button?

Right now I'm trying to test my entry widget by simply printing the value it holds but .get() seems to not be working. What am I overlooking?

from tkinter import *  

class Game(Frame):  
    'Number guessing application'  
    def __init__(self,parent=None):  
        'constructor'  
        Frame.__init__(self, parent)  
        self.pack()
        Game.make_widgets(self)
        Game.new_game(self)
        self.number = random.randrange(1,100)
        self.pack()

    def make_widgets(self):
        self.entry = Entry(self, width = 25, bg = 'purple',fg= 'green')
        self.entry.pack()
        self.button = Button(self,text = 'Enter',relief = 'ridge',command = self.reply())
        self.button.pack()
    def new_game(self):
        pass

    def reply(self):
       print(self.entry.get())

Game().mainloop()

Upvotes: 1

Views: 826

Answers (1)

Tim
Tim

Reputation: 12174

You have command = self.reply(), which calls self.reply() and uses the return value (in this case an implicit None) as the action.

Change it to command = self.reply, which will make it call the reply method every time.

Upvotes: 1

Related Questions