[Tutor] Events in tkinter

Abel Daniel abli@freemail.hu
Mon Mar 31 14:43:02 2003


Diego Prestes wrote:
> How can I create the following event using classes?
> 
> I want to type "control o", for example, and it call this function.
The event sequence you need is <Control-KeyPress-o>. I guess you already
have some widgets, so do a
widget.bind_all('<Control-KeyPress-o>', open)
at the same place where you create the widget. (Where widget is any
Tkinter widget you created.) This will bind the event to all the widgets,
so it will work whichever widget has the focus. I guess thats what you
want for a general short-cut like this. You can also bind to a specifc
widget with widget.bind( ) or to all instance of a class with
widget.bind_class( ).

The callback (in your case open) will get an event instance as an
argument, so it should accept it:

  def open(self, event): 
>     a = tkFileDialog.askopenfilename(title="Open", initialdir="/")
>     self.text = open(a).read() 
>     self.text1.delete(0.0,END)
>     self.text1.insert(END, self.text)

Abel Daniel

p.s.
looks like <Control-o> also works instead of <Control-KeyPress-o> (I
don't think there is a difference).

"Tkinter reference: a GUI for Python" (84 pp.) is a pretty extensive
reference. You can get it from
http://www.nmt.edu/tcc/help/lang/python/tkinter.html
(look for it in the 'Local Links' section).
At the end it has 8 pages about events in Tkinter.