[Tkinter-discuss] event_generate question

Allen Taylor Allen.Taylor at mdacorporation.com
Thu Jan 29 17:24:25 CET 2009

Hello Guilherme,

> >>> Guilherme Polo <ggpolo at gmail.com> 2009-01-28 18:18 >>>
> On Wed, Jan 28, 2009 at 7:54 PM, Allen Taylor
> <Allen.Taylor at mdacorporation.com> wrote:
> > I'm trying to generate some events in Tkinter but it doesn't seem to be
> > working the way I expected. Here's an example of what I'm trying to do...
> >
> > def click():
> >   print 'click'
> >
> > from Tkinter import *
> > top=Tk()
> > b=Button(top, text='Hit me', command=click)
> >
> > b.event_generate('<Button-1>', x=0, y=0)
> > b.event_generate('<ButtonRelease-1>', x=0, y=0)
> >
> > The appearance of the button changes after the <Button-1> event (to
> > depressed), and again after the <ButtonRelease-1> event (to normal again),
> > but the associated button command (the click function) is not called.
> Weren't you the guy who found something else to run these kind of tests ? :P
> If it is you then apparently my earlier email about taking a look at
> the ttk tests got ignored, but it is fine.
On the contrary, I looked into ttk and found it wasn't what I was looking for. We are doing full application black box test, not unit testing. Besides, using ttk would require a porting effort to an application we don't want to make any changes to. Instead, I decided to bite the bullet and create a layer that exposes Tkinter widgets to the AT-SPI world so we can use dogtail, LDTP, Accerciser, at-poke, or whatever. The new layer is called Tka11y (a11y = Accessibility).
> You will want to generate an <Enter> event before those ones, also, if
> your code is just that then also pack the button and wait for its
> visibility before generating the events.
Yes, my code did the pack (forgot to include it above), but I didn't know about the <Enter>. Adding this caused things to work. Thanks!
> > If I
> > bind a function for the event to the button using the bind method, it gets
> > called, but I really need the associated command to get called too.
> >
> > Also, I tried doing this with a Pmw.ScrolledListBox with an associated
> > dblclickcommand, so I tried to generate <Double-Button-1> and
> > <Double-ButtonRelease-1> events, but to my surprise, I received the
> > following error message for both cases:
> >
> > _tkinter.TclError: Double or Triple modifier not allowed
> Well, it makes some sense. How do you double click something ? I'm
> afraid you don't have a special key to double click, instead you just
> click twice (if you happen to have one then it just simulates the
> double click for you).
I tried this with the ScrolledListBox. Here's what I'm doing to simulate the double click. It seems to work fine.
    slb = Pmw.ScrolledListBox(top, dblclickcommand=click) # Same click() as above
    slb.pack() # I didn't forget this time :-)
    lb = slb.component('listbox') # Must generate event for subordinate listbox widget
    lb.event_generate('<Motion>',x=0,y=0) # Probably not needed, but it reflects reality more closely
    for i in range(2):
    lb.event_generate('<Leave>') # For good measure
Notably, the event must be generated for the contained listbox widget. If the event is sent to the ScrolledListBox itself, it doesn't work.
> In resume:
> import Tkinter
> root = Tkinter.Tk()
> def click():
>     print "click"
> def test(event):
>     print "double"
>     return "break"
> btn = Tkinter.Button(command=click)
> btn.bind('<Double-Button-1>', test)
> btn.pack()
> btn.wait_visibility()
> btn.event_generate('<Enter>', x=0, y=0)
> btn.event_generate('<Button-1>', x=0, y=0)
> btn.event_generate('<ButtonRelease-1>', x=0, y=0)
> btn.event_generate('<Button-1>', x=0, y=0)
> btn.event_generate('<ButtonRelease-1>', x=0, y=0)
> >
> > These problems are getting in the way of my attempt to automate GUI testing
> > with Tk. Am I missing something?
> >
After making the appropriate changes to Tka11y, I am now able to generate the double click from, for example, Accerciser and the application responses appropriately. Thanks for the help.
Is there any good documentation on the event system? I've been using http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm, but obviously, there's not enough detail, especially for event_generate.
BTW, I will be releasing Tka11y soon (within a few weeks). I've carved out a spot at http://tkinter.unpythonic.net/wiki/Tka11y (accessible from the front page). Stay tuned...

> > (Ok, I really don't need the first case, because I can simple call the
> > button's invoke method. But in the second case, the associated action is
> > only invoked via the double click action.)kk
> >
> > Allen B. Taylor
> > MDA
> > 9445 Airport Road
> > Brampton, ON  L6S 4J3
> > 905-790-2800 ext. 4350
> > allen.taylor at mdacorporation.com 
> -- 
> -- Guilherme H. Polo Goncalves
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org 
> http://mail.python.org/mailman/listinfo/tkinter-discuss 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20090129/82a503b5/attachment.htm>

More information about the Tkinter-discuss mailing list