Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 695317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:56:48+00:00 2026-05-14T02:56:48+00:00

My goal: the user clicks a button. From the button pops up a two-level

  • 0

My goal: the user clicks a button. From the button pops up a two-level menu. The user clicks on something, and this triggers a callback which does stuff.

Here is a minimal example:

import wx

class MyApp(wx.App):
    def OnInit(self):
        frame = TestFrame(None, -1, "Hello from wxPython")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

class TestFrame(wx.Frame):
    def __init__(self, *args, **kw):
        wx.Frame.__init__(self, *args, **kw)

        sizer = wx.BoxSizer()
        button = wx.Button(self, label='Click me')
        sizer.Add(button)
        self.SetSizerAndFit(sizer)

        mainmenu = wx.Menu()
        next_id = 1000000
        submenus = {}

        for title in ['Submenu 1', 'Submenu 2', 'Submenu 3']:
            mit = wx.MenuItem(mainmenu, id=next_id, text=title)
            submenu = wx.Menu()
            mit.SetSubMenu(submenu)
            mainmenu.AppendItem(mit)

            next_id = next_id + 1
            submenus[title] = submenu

        items = [('Submenu 1', 'foo'),
                 ('Submenu 1', 'bar'),
                 ('Submenu 2', 'one'),
                 ('Submenu 2', 'two'),
                 ('Submenu 2', 'three'),
                 ('Submenu 3', 'zif'),
                 ('Submenu 3', 'zaf')]

        for title, item in items:
            submenu = submenus[title]
            mit = wx.MenuItem(submenu, id=next_id, text=item)
            submenu.AppendItem(mit)
            next_id = next_id + 1

            def callback(e, title=title, item=item):
                print 'Item clicked: %s, %s' % (title, item)

            self.Bind(wx.EVT_MENU, callback, mit)


        def show(e):
            self.PopupMenu(mainmenu, button.GetPosition())
        button.Bind(wx.EVT_BUTTON, show)

app = MyApp(0)
app.MainLoop()

Also:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>> wx.version()
'2.8.10.1 (msw-unicode)'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:56:49+00:00Added an answer on May 14, 2026 at 2:56 am

    You are handling ID generation yourself and in doing that mixing up IDs, anyway you do not need to generate IDs yourself use wx.NewId(), if you replace next_id with that it will work
    e.g.

    mit = wx.MenuItem(submenu, id=wx.NewId(), text=item)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal: each time a user selects text, and clicks a button, that text
Goal When a user clicks the button, the div in question will: slide down
So my goal is once a user clicks on the item from the dropdown
My goal is to have a jQuery datepicker pop up when the user clicks
I am trying to popup an alert when the user clicks the 'like' button
Goal: When a user click a exit button in the file > exit or
My Goal I am attempting to make a Java program in which a user
There are two user controls, which are built dynamically. In the first control, I
My goal is to let the user click on a specific location on a
My goal for now is to have a user search for other users who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.