Added the sample code from wxPython In Action to the samples dir
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@42925 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
50
wxPython/samples/wxPIA_book/Chapter-10/add_items.py
Normal file
50
wxPython/samples/wxPIA_book/Chapter-10/add_items.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Add Menu Items")
|
||||
p = wx.Panel(self)
|
||||
self.txt = wx.TextCtrl(p, -1, "new item")
|
||||
btn = wx.Button(p, -1, "Add Menu Item")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)
|
||||
|
||||
sizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
sizer.Add(self.txt, 0, wx.ALL, 20)
|
||||
sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20)
|
||||
p.SetSizer(sizer)
|
||||
|
||||
self.menu = menu = wx.Menu()
|
||||
simple = menu.Append(-1, "Simple menu item")
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, simple)
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
def OnAddItem(self, event):
|
||||
item = self.menu.Append(-1, self.txt.GetValue())
|
||||
self.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)
|
||||
|
||||
def OnNewItemSelected(self, event):
|
||||
wx.MessageBox("You selected a new item")
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
22
wxPython/samples/wxPIA_book/Chapter-10/create_just_menu.py
Normal file
22
wxPython/samples/wxPIA_book/Chapter-10/create_just_menu.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Simple Menu Example")
|
||||
p = wx.Panel(self)
|
||||
menuBar = wx.MenuBar()
|
||||
menu = wx.Menu()
|
||||
menuBar.Append(menu, "Left Menu")
|
||||
menu2 = wx.Menu()
|
||||
menuBar.Append(menu2, "Middle Menu")
|
||||
menu3 = wx.Menu()
|
||||
menuBar.Append(menu3, "Right Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
29
wxPython/samples/wxPIA_book/Chapter-10/create_simple_menu.py
Normal file
29
wxPython/samples/wxPIA_book/Chapter-10/create_simple_menu.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1, "Simple Menu Example")
|
||||
p = wx.Panel(self)
|
||||
menu = wx.Menu()
|
||||
simple = menu.Append(-1, "Simple menu item")
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, simple)
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Simple Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
46
wxPython/samples/wxPIA_book/Chapter-10/disable_item.py
Normal file
46
wxPython/samples/wxPIA_book/Chapter-10/disable_item.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import wx
|
||||
|
||||
ID_SIMPLE = wx.NewId()
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Enable/Disable Menu Example")
|
||||
p = wx.Panel(self)
|
||||
self.btn = wx.Button(p, -1, "Disable Item", (20,20))
|
||||
self.Bind(wx.EVT_BUTTON, self.OnToggleItem, self.btn)
|
||||
|
||||
menu = wx.Menu()
|
||||
menu.Append(ID_SIMPLE, "Simple menu item")
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, id=ID_SIMPLE)
|
||||
|
||||
menu.AppendSeparator()
|
||||
menu.Append(wx.ID_EXIT, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
def OnToggleItem(self, event):
|
||||
menubar = self.GetMenuBar()
|
||||
enabled = menubar.IsEnabled(ID_SIMPLE)
|
||||
menubar.Enable(ID_SIMPLE, not enabled)
|
||||
self.btn.SetLabel(
|
||||
(enabled and "Enable" or "Disable") + " Item")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
47
wxPython/samples/wxPIA_book/Chapter-10/fancy_items.py
Normal file
47
wxPython/samples/wxPIA_book/Chapter-10/fancy_items.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Fancier Menu Example")
|
||||
p = wx.Panel(self)
|
||||
menu = wx.Menu()
|
||||
|
||||
bmp = wx.Bitmap("open.png", wx.BITMAP_TYPE_PNG)
|
||||
item = wx.MenuItem(menu, -1, "Has Open Bitmap")
|
||||
item.SetBitmap(bmp)
|
||||
menu.AppendItem(item)
|
||||
|
||||
if True or 'wxMSW' in wx.PlatformInfo:
|
||||
font = wx.SystemSettings.GetFont(
|
||||
wx.SYS_DEFAULT_GUI_FONT)
|
||||
font.SetWeight(wx.BOLD)
|
||||
item = wx.MenuItem(menu, -1, "Has Bold Font")
|
||||
item.SetFont(font)
|
||||
menu.AppendItem(item)
|
||||
|
||||
item = wx.MenuItem(menu, -1, "Has Red Text")
|
||||
item.SetTextColour("red")
|
||||
menu.AppendItem(item)
|
||||
|
||||
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
52
wxPython/samples/wxPIA_book/Chapter-10/find_item.py
Normal file
52
wxPython/samples/wxPIA_book/Chapter-10/find_item.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Find Item Example")
|
||||
p = wx.Panel(self)
|
||||
self.txt = wx.TextCtrl(p, -1, "new item")
|
||||
btn = wx.Button(p, -1, "Add Menu Item")
|
||||
self.Bind(wx.EVT_BUTTON, self.OnAddItem, btn)
|
||||
|
||||
sizer = wx.BoxSizer(wx.HORIZONTAL)
|
||||
sizer.Add(self.txt, 0, wx.ALL, 20)
|
||||
sizer.Add(btn, 0, wx.TOP|wx.RIGHT, 20)
|
||||
p.SetSizer(sizer)
|
||||
|
||||
self.menu = menu = wx.Menu()
|
||||
simple = menu.Append(-1, "Simple menu item")
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, simple)
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
def OnAddItem(self, event):
|
||||
item = self.menu.Append(-1, self.txt.GetValue())
|
||||
self.Bind(wx.EVT_MENU, self.OnNewItemSelected, item)
|
||||
|
||||
def OnNewItemSelected(self, event):
|
||||
item = self.GetMenuBar().FindItemById(event.GetId())
|
||||
text = item.GetText()
|
||||
wx.MessageBox("You selected the '%s' item" % text)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
BIN
wxPython/samples/wxPIA_book/Chapter-10/open.png
Normal file
BIN
wxPython/samples/wxPIA_book/Chapter-10/open.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 281 B |
49
wxPython/samples/wxPIA_book/Chapter-10/popupmenu.py
Normal file
49
wxPython/samples/wxPIA_book/Chapter-10/popupmenu.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Popup Menu Example")
|
||||
self.panel = p = wx.Panel(self)
|
||||
menu = wx.Menu()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
wx.StaticText(p, -1,
|
||||
"Right-click on the panel to show a popup menu",
|
||||
(25,25))
|
||||
|
||||
self.popupmenu = wx.Menu()
|
||||
for text in "one two three four five".split():
|
||||
item = self.popupmenu.Append(-1, text)
|
||||
self.Bind(wx.EVT_MENU, self.OnPopupItemSelected, item)
|
||||
p.Bind(wx.EVT_CONTEXT_MENU, self.OnShowPopup)
|
||||
|
||||
|
||||
def OnShowPopup(self, event):
|
||||
pos = event.GetPosition()
|
||||
pos = self.panel.ScreenToClient(pos)
|
||||
self.panel.PopupMenu(self.popupmenu, pos)
|
||||
|
||||
|
||||
def OnPopupItemSelected(self, event):
|
||||
item = self.popupmenu.FindItemById(event.GetId())
|
||||
text = item.GetText()
|
||||
wx.MessageBox("You selected item '%s'" % text)
|
||||
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
34
wxPython/samples/wxPIA_book/Chapter-10/sub_menu.py
Normal file
34
wxPython/samples/wxPIA_book/Chapter-10/sub_menu.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Sub-menu Example")
|
||||
p = wx.Panel(self)
|
||||
menu = wx.Menu()
|
||||
|
||||
submenu = wx.Menu()
|
||||
submenu.Append(-1, "Sub-item 1")
|
||||
submenu.Append(-1, "Sub-item 2")
|
||||
menu.AppendMenu(-1, "Sub-menu", submenu)
|
||||
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
36
wxPython/samples/wxPIA_book/Chapter-10/toggle_items.py
Normal file
36
wxPython/samples/wxPIA_book/Chapter-10/toggle_items.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Toggle Items Example")
|
||||
p = wx.Panel(self)
|
||||
menuBar = wx.MenuBar()
|
||||
menu = wx.Menu()
|
||||
exit = menu.Append(-1, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
menuBar.Append(menu, "Menu")
|
||||
|
||||
menu = wx.Menu()
|
||||
menu.AppendCheckItem(-1, "Check Item 1")
|
||||
menu.AppendCheckItem(-1, "Check Item 2")
|
||||
menu.AppendCheckItem(-1, "Check Item 3")
|
||||
menu.AppendSeparator()
|
||||
menu.AppendRadioItem(-1, "Radio Item 1")
|
||||
menu.AppendRadioItem(-1, "Radio Item 2")
|
||||
menu.AppendRadioItem(-1, "Radio Item 3")
|
||||
menuBar.Append(menu, "Toggle Items")
|
||||
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
49
wxPython/samples/wxPIA_book/Chapter-10/update_ui.py
Normal file
49
wxPython/samples/wxPIA_book/Chapter-10/update_ui.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import wx
|
||||
|
||||
ID_SIMPLE = wx.NewId()
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"UPDATE_UI Menu Example")
|
||||
p = wx.Panel(self)
|
||||
self.btn = wx.Button(p, -1, "Disable Item", (20,20))
|
||||
self.Bind(wx.EVT_BUTTON, self.OnToggleItem, self.btn)
|
||||
|
||||
menu = wx.Menu()
|
||||
menu.Append(ID_SIMPLE, "Simple menu item")
|
||||
self.enabled = True
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, id=ID_SIMPLE)
|
||||
self.Bind(wx.EVT_UPDATE_UI, self.OnUpdateSimple, id=ID_SIMPLE)
|
||||
|
||||
menu.AppendSeparator()
|
||||
menu.Append(wx.ID_EXIT, "Exit")
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
def OnToggleItem(self, event):
|
||||
self.btn.SetLabel(
|
||||
(self.enabled and "Enable" or "Disable") + " Item")
|
||||
self.enabled = not self.enabled
|
||||
|
||||
def OnUpdateSimple(self, event):
|
||||
event.Enable(self.enabled)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
47
wxPython/samples/wxPIA_book/Chapter-10/with_accelerator.py
Normal file
47
wxPython/samples/wxPIA_book/Chapter-10/with_accelerator.py
Normal file
@@ -0,0 +1,47 @@
|
||||
import wx
|
||||
|
||||
class MyFrame(wx.Frame):
|
||||
def __init__(self):
|
||||
wx.Frame.__init__(self, None, -1,
|
||||
"Accelerator Example")
|
||||
p = wx.Panel(self)
|
||||
menu = wx.Menu()
|
||||
simple = menu.Append(-1, "Simple &menu item") # with mnemonic
|
||||
accel = menu.Append(-1, "&Accelerated\tCtrl-A") # with accelerator
|
||||
|
||||
menu.AppendSeparator()
|
||||
exit = menu.Append(-1, "E&xit")
|
||||
|
||||
self.Bind(wx.EVT_MENU, self.OnSimple, simple)
|
||||
self.Bind(wx.EVT_MENU, self.OnAccelerated, accel)
|
||||
self.Bind(wx.EVT_MENU, self.OnExit, exit)
|
||||
|
||||
menuBar = wx.MenuBar()
|
||||
menuBar.Append(menu, "&Menu")
|
||||
self.SetMenuBar(menuBar)
|
||||
|
||||
# An alternate way to make accelerators
|
||||
acceltbl = wx.AcceleratorTable( [
|
||||
(wx.ACCEL_CTRL, ord('Q'), exit.GetId())
|
||||
])
|
||||
self.SetAcceleratorTable(acceltbl)
|
||||
|
||||
|
||||
def OnSimple(self, event):
|
||||
wx.MessageBox("You selected the simple menu item")
|
||||
|
||||
def OnAccelerated(self, event):
|
||||
wx.MessageBox("You selected the accelerated menu item")
|
||||
|
||||
|
||||
def OnExit(self, event):
|
||||
self.Close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = wx.PySimpleApp()
|
||||
frame = MyFrame()
|
||||
frame.Show()
|
||||
app.MainLoop()
|
||||
|
||||
|
Reference in New Issue
Block a user