Mark Summerfield | 21 Jun 2012 12:01

Problems with popup menus

Hi,

I am trying to create a popup menu that works like a pulldown menu, that
is, it:
    (1) supports up and down arrow navigation and Enter to select,
    (2) supports keyboard mnemonics (e.g., type 'b' to select Blue item),
    (3) is cancelled with Escape.

Here is an example that doesn't support any of the above (yet all of the
above work automatically for menus in a menu bar).

############################################################
# Adapted from
# http://www.java2s.com/Code/Python/GUI-Tk/Popupmenudemonstration.htm
from tkinter import *

class PopupMenuDemo(Frame):
   def __init__(self):
      Frame.__init__(self)
      self.pack(expand=YES, fill=BOTH)
      self.master.title("Popup Menu Demo")
      self.master.geometry("300x200")
      self.frame1 = Frame(self, bg="white")
      self.frame1.pack(expand= YES, fill=BOTH)
      self.popupMenu = Menu(self.frame1, tearoff=0)
      self.popupMenu.bind("<q>", self.popupMenu.unpost) # Doesn't work
      self.popupMenu.bind("<Escape>", self.popupMenu.unpost) # Doesn't work
      colors = ["White", "Red", "Green", "Blue"]
      self.selectedColor = StringVar()
      self.selectedColor.set(colors[0])
(Continue reading)

Mark Summerfield | 21 Jun 2012 12:20

Re: Problems with popup menus

Hi,

Silly me! The solution is not to do any manual bindings and instead of
calling Menu.post() call Menu.tk_popup() and everything works:-)

On Thu, 21 Jun 2012 11:01:07 +0100
Mark Summerfield <list <at> qtrac.plus.com> wrote:
> Hi,
> 
> I am trying to create a popup menu that works like a pulldown menu, that
> is, it:
>     (1) supports up and down arrow navigation and Enter to select,
>     (2) supports keyboard mnemonics (e.g., type 'b' to select Blue item),
>     (3) is cancelled with Escape.
> 
> Here is an example that doesn't support any of the above (yet all of the
> above work automatically for menus in a menu bar).
> 
> ############################################################
> # Adapted from
> # http://www.java2s.com/Code/Python/GUI-Tk/Popupmenudemonstration.htm
> from tkinter import *
> 
> class PopupMenuDemo(Frame):
>    def __init__(self):
>       Frame.__init__(self)
>       self.pack(expand=YES, fill=BOTH)
>       self.master.title("Popup Menu Demo")
>       self.master.geometry("300x200")
>       self.frame1 = Frame(self, bg="white")
(Continue reading)


Gmane