What is the difference between command and binding in tkinter?

I am trying to make a button to print a line when it is pressed, and print another when it is released. I know about the command and bind attributes, but I would like to know if it can only be executed using attributes or if I need to use methods. With this code snippet:

 class motor: def __init__(eleMesmo, eixo , valorZero): eleMesmo.eixo = eixo eleMesmo.zero = valorZero def aumenta(self): print(self.eixo + str(self.zero+5)) def diminui(self): print(self.eixo + str(self.zero-5)) def para(self): print(self.eixo + str(self.zero)) eixox = motor('x',90) eixoy = motor('y',90) class Interface: def __init__(elemesmo, widget): quadro = Frame(widget) quadro.pack() elemesmo.aumentarY = Button(quadro,text="Aumentar Y",height=10,width=20,command=eixoy.aumenta) elemesmo.aumentarY.pack(side=TOP) elemesmo.diminuirY = Button(quadro,text="Diminuir Y",height=10,width=20,command=eixoy.diminui) 

I can call the aumenta method on an eixo y object when the aumentarY button is aumentarY . I would like to call the para method on the eixo y object when the aumentarY button is released. How can i do this?

+1
source share
1 answer

All The types of events are indicated here , you are looking for <Button-1> (click on button 1 (left mouse button if you are right) and <ButtonRelease-1> (release mouse button 1 (left button if you are right))

note I would not use command if you link both of these files.

  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 should know that when using bind callback is called with Event , if you do not need it, you can simply add an optional and unused parameter for 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)) 
+3
source

Source: https://habr.com/ru/post/896592/


All Articles