source: wxmtxy/trunk/menuLauncher.py @ 177

Last change on this file since 177 was 177, checked in by jemian, 15 years ago

prepare menuLauncher to accept a command-line argument identifying a configuration file

  • Property svn:executable set to *
  • Property svn:keywords set to Date Revision Author HeadURL Id
File size: 5.0 KB
Line 
1#!/usr/bin/env python
2#Boa:Frame:Frame1
3
4#*************************************************************************
5# Copyright (c) 2009 The University of Chicago, as Operator of Argonne
6#     National Laboratory.
7# Copyright (c) 2009 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: 2010-06-02 17:52:24 +0000 (Wed, 02 Jun 2010) $
17# $Author: jemian $
18# $Revision: 177 $
19# $URL: wxmtxy/trunk/menuLauncher.py $
20# $Id: menuLauncher.py 177 2010-06-02 17:52:24Z jemian $
21########### SVN repository information ###################
22'''
23
24
25import wx
26import subprocess
27import os
28import pprint
29
30
31DEFAULT_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            ("separator", ),
40            ("xclock", "conventional analog clock", "xclock")
41        )
42
43
44def create(parent):
45    return Frame1(parent)
46
47
48[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL,
49] = [wx.NewId() for _init_ctrls in range(3)]
50
51
52[wxID_FRAME1THEMENUITEMS0, wxID_FRAME1THEMENUITEMS1,
53] = [wx.NewId() for _init_coll_theMenu_Items in range(2)]
54
55
56class Frame1(wx.Frame):
57
58    def _init_ctrls(self, prnt):
59        # generated method, don't edit
60        wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt,
61              pos=wx.Point(294, 331), size=wx.Size(223, 65),
62              style=wx.DEFAULT_FRAME_STYLE, title='menuLanucher')
63        self.SetClientSize(wx.Size(215, 31))
64
65        self.panel = wx.Panel(id=wxID_FRAME1PANEL, name='panel', parent=self,
66              pos=wx.Point(0, 0), size=wx.Size(215, 31),
67              style=wx.TAB_TRAVERSAL)
68
69        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1, label='button1 label',
70              name='button1', parent=self.panel, pos=wx.Point(0, 0),
71              size=wx.Size(216, 32), style=0)
72        self.button1.SetToolTipString('button1 tooltip')
73
74    def __init__(self, parent,
75            label="Command-line tools ...",
76            contents=None):
77        if contents == None:
78            contents = (
79                ("xeyes", "Here's looking at you, kid!", "xeyes"),
80                (None, ),
81                ("short name", "describe command to be done", "xclock")
82            )
83        self._init_ctrls(parent)
84        self.registry = {}
85        b = self.button1
86        b.SetLabel(label)
87        b.SetToolTipString(label)
88        b.Bind(wx.EVT_LEFT_DOWN, self.OnClick, b)
89        b.Bind(wx.EVT_RIGHT_DOWN, self.OnClick, b)
90        self.popupmenu = wx.Menu()
91        for entry in contents:
92            #pprint.pprint(entry)
93            if entry[0] == "separator":
94                self.appendSeparator(self.popupmenu)
95            else:
96                self.appendMenuItem(
97                    menu = self.popupmenu,
98                    id = wx.NewId(),
99                    text = entry[0],
100                    help = entry[1],
101                    cmd = entry[2],
102                    handler = self.OnTheMenuGenericMenu)
103
104    def appendSeparator(self, menu = None):
105        '''add a separator line to the menu'''
106        if menu != None:
107            menu.AppendSeparator()
108
109    def appendMenuItem(self, id, menu = None, text = 'text string',
110            help='help string', handler = None, cmd = ''):
111        '''add an item to the menu
112            @param id: (int) to associate with handler
113            @param menu: (menu object) usually self.theMenu
114            @param text: (string) label for the menu item
115            @param help: (string) describe actio to be performed
116            @param cmd: (string) full command to be called
117            @param handler: (method object) function that will receive event
118        '''
119        if menu != None:
120            entry = {}
121            entry['cmd'] = cmd
122            entry['text'] = text
123            entry['help'] = help
124            entry['id'] = id
125            self.registry[id] = entry
126            menu.Append(help=help, id=id, kind=wx.ITEM_NORMAL, text=text)
127            if handler != None:
128                self.Bind(wx.EVT_MENU, handler, id=id)
129
130    def OnTheMenuGenericMenu(self, event):
131        id = event.GetId()
132        entry = self.registry[id]
133        cmd = os.path.normpath(entry['cmd'])
134        #pprint.pprint(os.path.normpath(cmd))
135        subprocess.Popen(cmd, shell = True)
136        #print os.path.abspath(cmd)
137
138    def OnClick(self, event):
139        self.panel.PopupMenu(self.popupmenu, (0, 0))
140
141
142if __name__ == '__main__':
143    app = wx.PySimpleApp()
144    menu_items = DEFAULT_MENU_ITEMS
145    frame = Frame1(None, label="menuLauncher", contents=menu_items)
146    frame.Show()
147
148    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.