Re: Frame button binding

"johnmc" <[email protected]>
Newsgroups gmane.comp.python.tkinter
Message-ID <[email protected]>
On Tue, 6 Sep 2016 09:00:45 +0000, Vasilis Vlachoudis wrote
> Hi all,
> 
> I want to create a Scrolled Frame, it works nicely with the place command
> however when I bind the B2-Motion, and I click the middle mouse button
> on the child buttons instead of dragging it does nothing, since they are 
> capturing the B2 and not propagate it to the master.
> 
> SimplifiedExample:
> 
> def dragStart(e):
>     print "B2-pressed")
> 
> f = Frame(tk)
> f.pack(expand=YES, fill=BOTH)
> f.bind("<Button-2>", dragStart)
> 
> Button(f, text="Button").pack()
> 
> I found a not elegant solution of setting the bind to the toplevel like
> f.winfo_toplevel().bind("<Button-2>", dragStart)
> 
> and in dragStart I am checking
>      if not str(e.widget).startswith(str(f)): return
> 
> Is there a better way of doing it?
> 


Hi Vasilis,

I'm a bit late in this discussion, but have you tried the following (a modification 
of your simplified code):

from Tkinter import *

root = Tk()

def dragStart(e):
    print "B2-pressed"

def check(e):
    print "Button pressed with B2"
    return "break"
    
f = Frame(root)
f.pack(fill=BOTH, expand=YES)
root.bind("<Button-2>", dragStart)

b = Button(f, text='Button')
b.pack()
b.bind("<Button-2>", check)

root.mainloop()


In the above the <Button-2> event is bound to all the widgets in the Toplevel. There 
is also a separate binding specific to the Button widget.  If the <Button-2> event 
happens on the Button "b" widget it is not propagated to the higher binding level by 
means of the "break" return.

Regards,

John


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.