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])
      for color in colors:
         self.popupMenu.add_radiobutton(label=color,
            variable=self.selectedColor,
            underline=0,
            command=self.changeBackgroundColor)
      self.frame1.bind("<Button-3>", self.popUpMenu)

   def popUpMenu(self, event):
      self.popupMenu.post(event.x_root, event.y_root)

   def changeBackgroundColor(self):
      self.frame1.config(bg=self.selectedColor.get())
      
PopupMenuDemo().mainloop()   
############################################################

Any advice on this would be appreciated:-)

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Rapid GUI Programming with Python and Qt" - ISBN 0132354187
            http://www.qtrac.eu/pyqtbook.html
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to