Prev: The FASCINATING Secret Relationship Between PROFLIGATE WHITES and MISLEADER JEWS
Next: Changing the Emacs engine to Guile
From: Anthony Papillion on 22 Jun 2010 20:21 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
From: MRAB on 22 Jun 2010 20:39 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>'.
From: rantingrick on 22 Jun 2010 20:47 On Jun 22, 7:21 pm, Anthony Papillion <papill...(a)gmail.com> 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 --#
From: rantingrick on 22 Jun 2010 20:55
On Jun 22, 7:39 pm, MRAB <pyt...(a)mrabarnett.plus.com> 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! |