What is the difference between command and bind in tkinter?

醉酒当歌 提交于 2020-01-06 03:07:06

问题


I'm trying to make a button print a string when it's pressed and print another when it's released. I know about the command atribute and the bind method, but I would like to know if it's possible to accomplish it only using atributes or if I have to use methods. With this piece of code:

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 method aumenta for object eixo y when button aumentarY is pressed. I would like to call the method para for object eixo y when button aumentarY is released. How I can do it?


回答1:


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))



回答2:


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.



来源:https://stackoverflow.com/questions/36726716/what-is-the-difference-between-command-and-bind-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!