gpt4 book ai didi

python - WxPython、弹出菜单、回调和 Windows XP

转载 作者:行者123 更新时间:2023-12-01 06:17:22 25 4
gpt4 key购买 nike

我的目标:用户单击按钮。从按钮弹出一个两级菜单。用户单击某些内容,这会触发执行某些操作的回调。

这是一个最小的例子:

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()

另外:

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)'

最佳答案

您正在自己处理 ID 生成,并在混合 ID 的过程中,无论如何,您不需要使用 wx.NewId() 自己生成 ID,如果您将 next_id 替换为它,它将起作用例如

mit = wx.MenuItem(submenu, id=wx.NewId(), text=item)

关于python - WxPython、弹出菜单、回调和 Windows XP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2504094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com