What is the difference between command and bind in tkinter? What is the difference between command and bind in tkinter? tkinter tkinter

What is the difference between command and bind in tkinter?


All Event types are outlined here, you are looking for <Button-1> (click down on button 1 (left mouse button if you are right handed)) and <ButtonRelease-1> (release mouse button 1 (left button if you are right handed)).

Note I wouldn't use command if you bind both of these.

 elemesmo.aumentarY = Button(quadro,text="Aumentar Y",height=10,width=20) elemesmo.aumentarY.bind("<Button-1>",eixoy.aumenta) elemesmo.aumentarY.bind("<ButtonRelease-1>",eixoy.para)

However you must know that when using bind the callback is called with an Event object, if you don't need it you can just add an optional and unused parameter to the callback:

 def aumenta(self, event=None):     print(self.eixo + str(self.zero+5)) def diminui(self, event=None):     print(self.eixo + str(self.zero-5)) def para(self, event=None):    print(self.eixo + str(self.zero))


Both command and .bind method are used to add life and functionality to a button, using the tkinter module on Python.

Command Parameter:

tkinter.Button(frame, text="Exit", fg="green", bg="pink", command=master.destroy)

The above button would destroy the frame we the 'Exit' button is selected or clicked any how. Try using the tab key and pressing spacebar. You will notice that the button would work. What if you don't want that? What if you strictly want the user to click it using the left mouse button?Note that, you can pass a simple zero-parameter method to the command parameter, which may or may not contain any event object.

Bind Method:

The bind method is used to add extra information/functionality to the button, the way it needs to be clicked, the specific button that needs to be used and so on. It looks something like this:

btn = Button(frame, text="Exit", fg="green", bg="pink")btn.bind(sequence="<Button-1>", func=master.destroy)

This will work only when the Mouse Button-1 (Left-Mouse Button) is pressed. Some alternate versions to this are like:

btn.bind(sequence="<ButtonRelease-1>", func=master.destroy)

The above works when the Mouse Button-1 is pressed and released.

btn.bind(sequence="<Double-Button-1>", func=master.destroy)

Similarly, the above sequence works only when the Mouse Button-1 is Double Clicked.

Note that, in the bind method you cannot pass a simple zero-parameter method to the bind method, it must contain one event object as shown below.

def callback(event):          pass

For the entire list of sequences for bind method, refer this article.