1 | #!/usr/bin/env python |
---|
2 | #Boa:Frame:Frame1 |
---|
3 | |
---|
4 | #************************************************************************* |
---|
5 | # Copyright (c) 2009-2010 The University of Chicago, as Operator of Argonne |
---|
6 | # National Laboratory. |
---|
7 | # Copyright (c) 2009-2010 The Regents of the University of California, as |
---|
8 | # Operator of Los Alamos National Laboratory. |
---|
9 | # This file is distributed subject to a Software License Agreement found |
---|
10 | # in the file LICENSE that is included with this distribution. |
---|
11 | #************************************************************************* |
---|
12 | |
---|
13 | '''menuLauncher: Launch command-line statements from a wxPython menu |
---|
14 | |
---|
15 | ########### SVN repository information ################### |
---|
16 | # $Date: 2011-09-09 17:12:01 +0000 (Fri, 09 Sep 2011) $ |
---|
17 | # $Author: jemian $ |
---|
18 | # $Revision: 631 $ |
---|
19 | # $URL: wxmtxy/trunk/src/moxy/menuLauncher.py $ |
---|
20 | # $Id: menuLauncher.py 631 2011-09-09 17:12:01Z jemian $ |
---|
21 | ########### SVN repository information ################### |
---|
22 | ''' |
---|
23 | |
---|
24 | |
---|
25 | import wx |
---|
26 | import subprocess |
---|
27 | import os |
---|
28 | import pprint |
---|
29 | |
---|
30 | |
---|
31 | DEFAULT_MENU_ITEMS = ( |
---|
32 | ("xcalc RPN", "HP-style", "xcalc -rpn"), |
---|
33 | ("xcalc", "TI-style", "xcalc"), |
---|
34 | ("gcalc", "Gnome calculator", "gcalctool"), |
---|
35 | ("nedit-client", "window editor", "nedit-client"), |
---|
36 | ("nedit-nc", "window editor", "nedit-nc"), |
---|
37 | ("xman", "X11 man pages", "xman -bothshown"), |
---|
38 | ("xeyes", "Here's looking at you, kid!", "xeyes"), |
---|
39 | ("wxmtxy", "motor tool", "/APSshare/bin/python ./wxmtxy.py"), |
---|
40 | ("separator", ), |
---|
41 | ("xclock", "conventional analog clock", "xclock") |
---|
42 | ) |
---|
43 | |
---|
44 | |
---|
45 | def create(parent): |
---|
46 | return Frame1(parent) |
---|
47 | |
---|
48 | |
---|
49 | [wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL, |
---|
50 | ] = [wx.NewId() for _init_ctrls in range(3)] |
---|
51 | |
---|
52 | |
---|
53 | [wxID_FRAME1THEMENUITEMS0, wxID_FRAME1THEMENUITEMS1, |
---|
54 | ] = [wx.NewId() for _init_coll_theMenu_Items in range(2)] |
---|
55 | |
---|
56 | |
---|
57 | class Frame1(wx.Frame): |
---|
58 | |
---|
59 | def _init_ctrls(self, prnt): |
---|
60 | # generated method, don't edit |
---|
61 | wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, |
---|
62 | pos=wx.Point(294, 331), size=wx.Size(223, 65), |
---|
63 | style=wx.DEFAULT_FRAME_STYLE, title='menuLanucher') |
---|
64 | self.SetClientSize(wx.Size(215, 31)) |
---|
65 | |
---|
66 | self.panel = wx.Panel(id=wxID_FRAME1PANEL, name='panel', parent=self, |
---|
67 | pos=wx.Point(0, 0), size=wx.Size(215, 31), |
---|
68 | style=wx.TAB_TRAVERSAL) |
---|
69 | |
---|
70 | self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1 label', |
---|
71 | name='button1', parent=self.panel, pos=wx.Point(0, 0), |
---|
72 | size=wx.Size(216, 32), style=0) |
---|
73 | self.button1.SetToolTipString('button1 tooltip') |
---|
74 | |
---|
75 | def __init__(self, parent, |
---|
76 | label="Command-line tools ...", |
---|
77 | contents=None): |
---|
78 | if contents == None: |
---|
79 | contents = ( |
---|
80 | ("xeyes", "Here's looking at you, kid!", "xeyes"), |
---|
81 | (None, ), |
---|
82 | ("short name", "describe command to be done", "xclock") |
---|
83 | ) |
---|
84 | self._init_ctrls(parent) |
---|
85 | self.registry = {} |
---|
86 | b = self.button1 |
---|
87 | b.SetLabel(label) |
---|
88 | b.SetToolTipString(label) |
---|
89 | b.Bind(wx.EVT_LEFT_DOWN, self.OnClick, b) |
---|
90 | b.Bind(wx.EVT_RIGHT_DOWN, self.OnClick, b) |
---|
91 | self.popupmenu = wx.Menu() |
---|
92 | for entry in contents: |
---|
93 | #pprint.pprint(entry) |
---|
94 | if entry[0] == "separator": |
---|
95 | self.appendSeparator(self.popupmenu) |
---|
96 | else: |
---|
97 | self.appendMenuItem( |
---|
98 | menu = self.popupmenu, |
---|
99 | id = wx.NewId(), |
---|
100 | text = entry[0], |
---|
101 | help = entry[1], |
---|
102 | cmd = entry[2], |
---|
103 | handler = self.OnTheMenuGenericMenu) |
---|
104 | |
---|
105 | def appendSeparator(self, menu = None): |
---|
106 | '''add a separator line to the menu''' |
---|
107 | if menu != None: |
---|
108 | menu.AppendSeparator() |
---|
109 | |
---|
110 | def appendMenuItem(self, id, menu = None, text = 'text string', |
---|
111 | help='help string', handler = None, cmd = ''): |
---|
112 | '''add an item to the menu |
---|
113 | @param id: (int) to associate with handler |
---|
114 | @param menu: (menu object) usually self.theMenu |
---|
115 | @param text: (string) label for the menu item |
---|
116 | @param help: (string) describe actio to be performed |
---|
117 | @param cmd: (string) full command to be called |
---|
118 | @param handler: (method object) function that will receive event |
---|
119 | ''' |
---|
120 | if menu != None: |
---|
121 | entry = {} |
---|
122 | entry['cmd'] = cmd |
---|
123 | entry['text'] = text |
---|
124 | entry['help'] = help |
---|
125 | entry['id'] = id |
---|
126 | self.registry[id] = entry |
---|
127 | menu.Append(help=help, id=id, kind=wx.ITEM_NORMAL, text=text) |
---|
128 | if handler != None: |
---|
129 | self.Bind(wx.EVT_MENU, handler, id=id) |
---|
130 | |
---|
131 | def OnTheMenuGenericMenu(self, event): |
---|
132 | id = event.GetId() |
---|
133 | entry = self.registry[id] |
---|
134 | cmd = os.path.normpath(entry['cmd']) |
---|
135 | #pprint.pprint(os.path.normpath(cmd)) |
---|
136 | subprocess.Popen(cmd, shell = True) |
---|
137 | #print os.path.abspath(cmd) |
---|
138 | |
---|
139 | def OnClick(self, event): |
---|
140 | self.panel.PopupMenu(self.popupmenu, (0, 0)) |
---|
141 | |
---|
142 | |
---|
143 | if __name__ == '__main__': |
---|
144 | app = wx.PySimpleApp() |
---|
145 | menu_items = DEFAULT_MENU_ITEMS |
---|
146 | frame = Frame1(None, label="menuLauncher", contents=menu_items) |
---|
147 | frame.Show() |
---|
148 | |
---|
149 | app.MainLoop() |
---|