0

I have difficulty understanding why this very simple piece of code doesn't work. Basically it's supposed to print anything out you type. It runs without errors but when I type something into the entry widget and press the submit button it doesn't print anything out. I'm using Python 3.xx.

from tkinter import *

window = Tk()

def GET():
    typed = e.get()
    print(typed)

e = Entry(window)
e.pack()
b = Button(window, text = "Submit", command = GET())
b.pack()
window.mainloop()
Cœur
  • 37,241
  • 25
  • 195
  • 267
Vitalynx
  • 946
  • 1
  • 8
  • 17

2 Answers2

0

What you need to do is set the command to GET instead of GET(). All you need to do is pass is the reference, not the full function invocation, because that then passes on the return value:

from tkinter import *

window = Tk()

def GET():
    typed = e.get()
    print(typed)

e = Entry(window)
e.pack()
b = Button(window, text = "Submit", command = GET) # GET not GET()
b.pack()
window.mainloop()

Now, it will execute GET accordingly. The callback only needs the reference of the function, not a function invocation which will get the return value. This is None in this instance and makes the button do nothing.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • You're right, could you specifiy what you mean with return value? I'm new to python – Vitalynx Sep 10 '16 at 15:15
  • When you call a function, you can return a value to the callee. Basically, a function that does an operation, then returns a value. Since your function *has no return statement*, it's None. Here's a [doc](https://docs.python.org/2/reference/simple_stmts.html#the-return-statement) link. – Andrew Li Sep 10 '16 at 15:16
  • Alright, thanks for the help! – Vitalynx Sep 10 '16 at 15:25
0

b = Button(window, text = "Submit", command = GET())

Note that by doing command=GET() you are calling the function GET and then passing its return value (None in this case) to the command argument.

Instead, you should be doing command=GET. This will pass the function GET to the command argument rather than its return value.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154