TkInter bind() event is not firing event trigger

A

Anthony Papillion

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
 
M

MRAB

Anthony said:
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>'.
 
R

rantingrick

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 --#
 
R

rantingrick


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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top