Daniel
Daniel

Reputation: 69

.get() not working correctly with entry boxes in tkinter

game_entry = Entry(gs, width = 10, bg = 'white')
game_entry.grid(row=4, column = 0, sticky=W)
ip = game_entry.get()
part1 = partial(click2, ip)
Button(gs, text = "Submit", width = 6, command = part1, bg='white').grid(row=5, column = 0, sticky=W)
output = Text(gs, width = 25, height = 1, wrap=WORD,bg = 'white')
output.grid(row = 7,column=0,sticky=W)

def click2(a):
    if a == 'a':
        print('hello')

If I click the button and the entry box says 'a' (without the quote marks), nothing gets printed and if i define click2 as:

def click2(a):
    if a != 'a':
        print('hello')

and type 'a' (again, without quote marks), it does print it even thought it shouldn't.

Does anyone know why?

Thanks

Upvotes: 0

Views: 72

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385830

The problem is because you get the value of the entry widget about a millisecond after you create the entry widget. You need to wait until the user clicks the button before you call the .get() method.

Upvotes: 2

Related Questions