[Tutor] events and popup menus

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Tutor] events and popup menus

Chris Hare, CISSP, CISA, CISM

I have four images in a frame.  I want to pop up a menu when the user right clicks on an image, and when they choose an option from the menu, execute the action.  

I can create the popup menu, and bind it to the image.  However, what I can't figure out is how to detect in the popup menu code which image fired the event so I can do the right thing (like display a larger version of the image, etc.)

# create a menu
self.popup = Menu(self.pictureWindow, tearoff=0)
 self.popup.add_command(label="Change Picture", command=self.selectPicture)
 self.popup.add_command(label="Make Primary", command=self.selectPicture)
 self.popup.add_command(label="Large View", command=self.selectPicture)

self.picture1.bind("<Button-1>", self.do_popup)

 def do_popup(self,event):
            # display the popup menu
          try:
           self.popup.tk_popup(event.x_root, event.y_root, 0)

                 finally:
                         # make sure to release the grab (Tk 8.0a1 only)
                         self.popup.grab_release()

Thanks for the advice!

Chris


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] events and popup menus

Prasad, Ramit-2
> I have four images in a frame.  I want to pop up a menu when the user
> right clicks on an image, and when they choose an option from the menu,
> execute the action.
>
> I can create the popup menu, and bind it to the image.  However, what I
> can't figure out is how to detect in the popup menu code which image fired
> the event so I can do the right thing (like display a larger version of
> the image, etc.)
>
> # create a menu
> self.popup = Menu(self.pictureWindow, tearoff=0)
>  self.popup.add_command(label="Change Picture",
> command=self.selectPicture)
>  self.popup.add_command(label="Make Primary", command=self.selectPicture)
>  self.popup.add_command(label="Large View", command=self.selectPicture)
>
> self.picture1.bind("<Button-1>", self.do_popup)
>
>  def do_popup(self,event):
>             # display the popup menu
>           try:
>            self.popup.tk_popup(event.x_root, event.y_root, 0)
>
>                  finally:
>                          # make sure to release the grab (Tk 8.0a1 only)
>                          self.popup.grab_release()
>

I would probably use a lambda to pass the picture name (where LARGE_VIEW is a module level unique string identifier).

self.popup.add_command(label=LARGE_VIEW, command=lambda event: self.selectPicture( LARGE_VIEW) )

Or if you need the event in selectPicture

self.popup.add_command(label=LARGE_VIEW, command=lambda event: self.selectPicture( LARGE_VIEW, event) )


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [Tutor] events and popup menus

Alan Gauld
In reply to this post by Chris Hare, CISSP, CISA, CISM
On 01/05/12 21:59, Chris Hare wrote:
> ... what I can't figure out is how to detect in the popup menu code
 > which image fired the event


>   def do_popup(self,event):


The event argument has various attributes. For a mouse click it should
include the screen coordinates. You can use those to determine which
widget was being clicked.

HTH

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Loading...