[Tkinter-discuss] event_generate question

Guilherme Polo ggpolo at gmail.com
Thu Jan 29 00:18:35 CET 2009

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.

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.

> 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).

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.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?
> (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.)
> Allen B. Taylor
> 9445 Airport Road
> Brampton, ON  L6S 4J3
> 905-790-2800 ext. 4350
> allen.taylor at mdacorporation.com

-- Guilherme H. Polo Goncalves

More information about the Tkinter-discuss mailing list