tkinter button widget

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Elaine Jackson

    tkinter button widget

    I've got a script where a button gets pushed over and over: to cut down on the
    carpal tunnel syndrome I'd like to have the button respond to presses of the
    Enter key as well as mouse clicks; can somebody clue me in regarding how this is
    done? Muchas gracias.

    Peace


  • Jeff Epler

    #2
    Re: tkinter button widget

    You'll have to arrange for the widget with keyboard focus to have a
    binding for the "<Return>" event ("<Enter>" is a valid event name, but
    it refers to the event generated when the mouse pointer enters a
    widget). The called function would call the invoke() method on the
    button.

    You can create a binding on all widgets within a given toplevel by
    making the binding on the toplevel itself.

    Example:

    import Tkinter

    def c():
    print "button invoked"

    t = Tkinter.Tk()
    b = Tkinter.Button( t, text="Do the thing", command=c)
    t.bind("<Return >", lambda event: b.invoke())
    e = Tkinter.Entry()
    e.pack()
    b.pack(anchor=T kinter.E)
    t.mainloop()

    Jeff

    Comment

    • Jeff Epler

      #3
      Re: tkinter button widget

      PS To indicate to the user that hitting the Enter key will invoke a particular
      button, create the widget with default="active ".

      Jeff

      Comment

      • klappnase

        #4
        Re: tkinter button widget

        "Elaine Jackson" <elainejackson7 355@home.com> wrote in message news:<Ygxpc.492 595$Ig.75690@pd 7tw2no>...[color=blue]
        > I've got a script where a button gets pushed over and over: to cut down on the
        > carpal tunnel syndrome I'd like to have the button respond to presses of the
        > Enter key as well as mouse clicks; can somebody clue me in regarding how this is
        > done? Muchas gracias.
        >
        > Peace[/color]

        b = Button(master, command=do_some thing)
        b.bind('<Return >', lambda event, key='<space>' : b.event_generat e(key))

        I hope this helped

        Michael

        Comment

        • Elaine Jackson

          #5
          Re: tkinter button widget

          Thanks for replying, but what you suggest doesn't seem to be working. Nothing I
          try gets the button to have focus in the first place. If I omit the part
          corresponding to

          e = Tkinter.Entry()
          e.pack()
          b.pack(anchor=T kinter.E)

          then nothing happens, but if I include it, it's an error. Maybe you can point me
          toward some kind of online resource? My favorite would be to get the knowledge
          required for this one trick (invoking a button's function with a keypress
          instead of a mouse click) without climbing any more of the Tkinter learning
          curve (for now) than I need to.

          Peace


          "Jeff Epler" <jepler@unpytho nic.net> wrote in message
          news:mailman.0. 1084663672.1360 8.python-list@python.org ...
          | You'll have to arrange for the widget with keyboard focus to have a
          | binding for the "<Return>" event ("<Enter>" is a valid event name, but
          | it refers to the event generated when the mouse pointer enters a
          | widget). The called function would call the invoke() method on the
          | button.
          |
          | You can create a binding on all widgets within a given toplevel by
          | making the binding on the toplevel itself.
          |
          | Example:
          |
          | import Tkinter
          |
          | def c():
          | print "button invoked"
          |
          | t = Tkinter.Tk()
          | b = Tkinter.Button( t, text="Do the thing", command=c)
          | t.bind("<Return >", lambda event: b.invoke())
          | e = Tkinter.Entry()
          | e.pack()
          | b.pack(anchor=T kinter.E)
          | t.mainloop()
          |
          | Jeff
          |


          Comment

          • Jeff Epler

            #6
            Re: tkinter button widget

            It works just dandy here, and since you didn't provide the error text
            there's really not much I can do for you.

            Jeff

            Comment

            • Elaine Jackson

              #7
              Re: tkinter button widget

              Sorry, that was 'my bad'. It actually does work. Thanks.

              "Jeff Epler" <jepler@unpytho nic.net> wrote in message
              news:mailman.7. 1084710699.1360 8.python-list@python.org ...
              | It works just dandy here, and since you didn't provide the error text
              | there's really not much I can do for you.
              |
              | Jeff
              |


              Comment

              Working...