[myphp]
#!/usr/bin/env python
# -*- coding:gb2312 -*-
#导入必要的模块
from wxPython.wx import *
text = u"这是一个弹出菜单的示例"
class TestPanel(wxPanel):
def __init__(self, parent):
wxPanel.__init__(self, parent, -1)
#定义一个布局管理器
box = wxBoxSizer(wxVERTICAL)
fs = self.GetFont().GetPointSize()
bf = wxFont(fs+4, wxSWISS, wxNORMAL, wxBOLD)
nf = wxFont(fs+2, wxSWISS, wxNORMAL, wxNORMAL)
t = wxStaticText(self, -1, u"弹出菜单")
t.SetFont(bf)
box.Add(t, 0, wxCENTER|wxALL, 5)
box.Add(wxStaticLine(self, -1), 0, wxEXPAND)
box.Add(10, 20)
t = wxStaticText(self, -1, text)
t.SetFont(nf)
box.Add(t, 0, wxCENTER|wxALL, 5)
self.SetSizer(box)
EVT_RIGHT_UP(self, self.OnRightClick)
def OnRightClick(self, event):
self.popupid1 = wxNewId()
self.popupid2 = wxNewId()
self.popupid3 = wxNewId()
self.popupid2 = wxNewId()
self.popupid3 = wxNewId()
self.popupid4 = wxNewId()
self.popupid5 = wxNewId()
self.popupid6 = wxNewId()
self.popupid7 = wxNewId()
self.popupid8 = wxNewId()
self.popupid9 = wxNewId()
#制作菜单
menu = wxMenu()
#关于如何放置一个图标文件到菜单项中
item = wxMenuItem(menu, self.popupid1, u"第一个选项")
menu.Append(self.popupid2, u"第二个选项")
menu.Append(self.popupid3, u"第三个选项")
menu.Append(self.popupid4, u"第四个选项")
menu.Append(self.popupid5, u"第五个选项")
menu.Append(self.popupid6, u"第六个选项")
sm = wxMenu()
sm.Append(self.popupid8, u"子菜单一")
sm.Append(self.popupid9, u"子菜单二")
menu.AppendMenu(self.popupid7, u"子菜单项检测", sm)
#显示弹出菜单了
self.PopupMenu(menu, event.GetPosition())
#显示完后需要将菜单desotry
menu.Destroy()
#定义一个frame类
class MyFrame(wxFrame):
def __init__(self):
wxFrame.__init__(self, None, -1, u"弹出菜单示例")
t = TestPanel(self)
#定义一个程序类
class MyApp(wxApp):
def OnInit(self):
class MyApp(wxApp):
def OnInit(self):
f = MyFrame()
f.Show(true)
self.SetTopWindow(f)
return true
#启动程序
if ( __name__ == "__main__" ):
a = MyApp(0)
a.MainLoop()
[/myphp]