TkInter bind() event is not firing event trigger

Discussion in 'Python' started by Anthony Papillion, Jun 23, 2010.

  1. So I want to execute some code when the user double clicks an item in
    a ListBox. The documentation says I should use the listbox.bind()
    method, specifying the Double-l event to detect the double left mouse
    button click. My code is this:

    gsItems = Listbox(root, width=76, height=30, selectmode="browse",
    yscroll=scrollBar.set)
    gsItems.bind('<Double-l>', openandDisplayFolder)
    gsItems.pack()

    So the first line configures the Listbox, second line SHOULD set up
    the event, and third line, of course, adds the Listbox to the UI.

    The problem is that the openandDisplayFolder function is never
    executed. The function is VERY simple for now while I learn the way to
    do it:

    def openandDisplayFolder(event):
    tkMessageBox.showinfo("Event Fired", "An item has been double
    clicked!")

    I've also removed the (event) parameter just in case and tried it and
    it makes no difference. What am I doing wrong here?

    Thanks!
    Anthony
    Anthony Papillion, Jun 23, 2010
    #1
    1. Advertising

  2. Anthony Papillion

    MRAB Guest

    Anthony Papillion wrote:
    > So I want to execute some code when the user double clicks an item in
    > a ListBox. The documentation says I should use the listbox.bind()
    > method, specifying the Double-l event to detect the double left mouse
    > button click. My code is this:
    >
    > gsItems = Listbox(root, width=76, height=30, selectmode="browse",
    > yscroll=scrollBar.set)
    > gsItems.bind('<Double-l>', openandDisplayFolder)
    > gsItems.pack()
    >
    > So the first line configures the Listbox, second line SHOULD set up
    > the event, and third line, of course, adds the Listbox to the UI.
    >
    > The problem is that the openandDisplayFolder function is never
    > executed. The function is VERY simple for now while I learn the way to
    > do it:
    >
    > def openandDisplayFolder(event):
    > tkMessageBox.showinfo("Event Fired", "An item has been double
    > clicked!")
    >
    > I've also removed the (event) parameter just in case and tried it and
    > it makes no difference. What am I doing wrong here?
    >

    According to:


    http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm

    it's '<Double-Button-1>'.
    MRAB, Jun 23, 2010
    #2
    1. Advertising

  3. Anthony Papillion

    rantingrick Guest

    On Jun 22, 7:21 pm, Anthony Papillion <> wrote:

    > I've also removed the (event) parameter just in case and tried it and
    > it makes no difference. What am I doing wrong here?


    Well don't remove the event parameter because Tkinter will pass it
    every time since this is an EVENT! Here is some code to play with.
    Note: If using Python 3.x use lowercase Tkinter!

    #-- Start Code --#

    import Tkinter as tk
    from Tkconstants import *

    class MyListbox(tk.Listbox):
    def __init__(self, master, **kw):
    tk.Listbox.__init__(self, master, **kw)
    self.bind("<Double-Button-1>", self.onButtonOneDoubleClick)
    self.bind("<Button-3>", self.onButtonThreeClick)

    def onButtonOneDoubleClick(self, event):
    lineno = self.nearest(event.y)
    print 'User double clicked line: %s' %(lineno)

    def onButtonThreeClick(self, event):
    lineno = self.nearest(event.y)
    self.activate(lineno)
    #
    # You make this next comand a bit smarter
    # and only clear the selection when user
    # clicks on a line this is not already selected
    # (well in multi line mode anyway), but i cannot
    # do all the work! ;)
    self.selection_clear(0, END)
    self.selection_set(lineno)
    print 'User right clicked line: %s' %(lineno)


    if __name__ == '__main__':
    app = tk.Tk()
    listbox = MyListbox(app, width=10, height=10, bg='white',
    selectmode=EXTENDED)
    for x in range(100):
    listbox.insert(END, x)
    listbox.pack(fill=BOTH, expand=1)
    app.mainloop()

    #-- End Code --#
    rantingrick, Jun 23, 2010
    #3
  4. Anthony Papillion

    rantingrick Guest

    On Jun 22, 7:39 pm, MRAB <> wrote:

    > http://www.pythonware.com/library/tkinter/introduction/events-and-bin...
    >
    > it's '<Double-Button-1>'.


    Yes and i vehemently hate these names! Just hideous if you ask me.

    @ Anthony
    Also be sure to use an "event name" for an event function/method and
    use the docstring to describe what the event will do. Do NOT use the
    method name to describe this like you have done. It's also a good idea
    to start event methods with "on". This is fairly well recognized as an
    event naming convention...

    onMotion()
    onButtonOneClick()
    onKeyPress()
    onEnter()
    onConfigure()
    ....etc

    ....Just ideas folks, just ideas!
    rantingrick, Jun 23, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,597
    mr2_93
    Oct 2, 2005
  2. =?Utf-8?B?TWFlIExpbQ==?=

    Urgent: Trigger javascript function after bind control

    =?Utf-8?B?TWFlIExpbQ==?=, Jun 7, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    531
    Bill Gregg
    Jun 7, 2006
  3. Replies:
    0
    Views:
    590
  4. Nathan Sokalski
    Replies:
    2
    Views:
    1,008
    Nathan Sokalski
    Nov 17, 2007
  5. Replies:
    0
    Views:
    382
Loading...

Share This Page