-1

So, I have the following code sample:

from Tkinter import *
import socket

def click(*args):
    sock = socket.socket()
    try:
        sock.connect(('localhost', 9999))
        sock.send(args)
    except socket.error:
        print 'server is not runing'
        pass


root = Tk()
root.bind("<Button-1>", click)
mainloop()

This looks pretty clear: you run the code, Tkinter window arrives, you click it, and it prints 'server is not runing', cos no server is running at 9999 port.

But if you change call of binded function from click to click("wtf") or even to click(), script will print message right after window appeared, before you actually click it.

Why such thing happens?

Kriattiffer
  • 617
  • 2
  • 8
  • 20
  • 1
    possible duplicate of a dozen or more questions, including [Calling functions with arguments on "command" and "bind"](http://stackoverflow.com/questions/9396211/calling-functions-with-arguments-on-command-and-bind). – Bryan Oakley Sep 16 '13 at 10:41

1 Answers1

2

Works as expected ;-) With the round braces the function 'click' will be called before root.bind gets executed. Without the braces 'click' will used just as a reference.

>>> def click(*args):
...     print "click"
...
>>> click
<function click at 0x022FECB0>
>>> click()
click
>>>
Robert Caspary
  • 1,584
  • 9
  • 7