Update of /cvsroot/pythoncard/PythonCard/samples/sysTray In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3111 Added Files: icon-off.ico icon-off.png icon-on.ico icon-on.png readme.txt sysTray.py sysTray.rsrc.py Log Message: New sample showing how to use the system tray in a PythonCard application --- NEW FILE: icon-off.ico --- (This appears to be a binary file; contents omitted.) --- NEW FILE: icon-off.png --- (This appears to be a binary file; contents omitted.) --- NEW FILE: icon-on.png --- (This appears to be a binary file; contents omitted.) --- NEW FILE: sysTray.rsrc.py --- {'application':{'type':'Application', 'name':'Template', 'backgrounds': [ {'type':'Background', 'name':'bgMain', 'title':u'sysTray Example', 'size':(325, 240), 'statusBar':1, 'menubar': {'type':'MenuBar', 'menus': [ {'type':'Menu', 'name':'menuFile', 'label':'&File', 'items': [ {'type':'MenuItem', 'name':'menuFileExit', 'label':'E&xit', }, ] }, ] }, 'components': [ {'type':'TextArea', 'name':'TextArea1', 'position':(5, 10), 'size':(311, 132), 'editable':False, 'text':u'This sample shows how to use a combination of raw wxPython and normal PythonCard code to produce an app which integrates into the system tray. The system tray icon has a context menu which pops up when the icon is right-clicked.', }, {'type':'Button', 'name':'bQuit', 'position':(235, 150), 'label':u'Quit', }, ] # end components } # end background ] # end backgrounds } } --- NEW FILE: sysTray.py --- #!/usr/bin/python # PythonCard example showing system tray integration # # Copyright (c) 2001-2005 PythonCard developers # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT # OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY # WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # # vim: ai et sw=4 ts=4 # standard imports import sys # pythoncard imports import wx from PythonCard import dialog, model, util class sysTray(model.Background): def on_initialize(self, event): #make the TaskBar icon self.tbIcon = wx.TaskBarIcon() if "wxMSW" in wx.PlatformInfo: self.icon0 = wx.Icon('icon-off.ico', wx.BITMAP_TYPE_ICO) self.icon1 = wx.Icon('icon-on.ico', wx.BITMAP_TYPE_ICO) elif "wxGTK" in wx.PlatformInfo: self.icon0 = wx.Icon('icon-off.png', wx.BITMAP_TYPE_PNG) self.icon1 = wx.Icon('icon-on.png', wx.BITMAP_TYPE_PNG) self.tbIcon.SetIcon(self.icon1, "PythonCard system tray sample") # build the popup context menu self.TBMENU_RESTORE = wx.NewId() self.TBMENU_CLOSE = wx.NewId() self.TBMENU_ITEM1 = wx.NewId() self.TBMENU_ITEM2 = wx.NewId() self.contextMenu = wx.Menu() self.contextMenu.Append(self.TBMENU_RESTORE, "Restore Window") self.contextMenu.AppendSeparator() self.contextMenu.Append(self.TBMENU_ITEM1, "Menu Item 1") self.contextMenu.Append(self.TBMENU_ITEM2, "Menu Item 2") self.contextMenu.AppendSeparator() self.contextMenu.Append(self.TBMENU_CLOSE, "Close Application") wx.EVT_TASKBAR_LEFT_DCLICK(self.tbIcon, self.on_TaskBarActivate) wx.EVT_TASKBAR_RIGHT_UP(self.tbIcon, self.on_TaskBarMenu) # bind some events to the menu items wx.EVT_MENU(self, self.TBMENU_RESTORE, self.on_TaskBarActivate) wx.EVT_MENU(self, self.TBMENU_CLOSE, self.on_TaskBarClose) wx.EVT_MENU(self, self.TBMENU_ITEM1, self.onContextMenu_Item1) wx.EVT_MENU(self, self.TBMENU_ITEM2, self.onContextMenu_Item2) self.reallyClose = False self.closeWarningSeen = False def onContextMenu_Item1(self, event): # these context menu event handlers don't work in quite the way I # expected on Linux. With the systray application minimized down to # the system tray, if you pop up the context menu and select item1 # or item2, the main window re-appears behind the alertDialog - this # happens on Linux but not on Windows. Dunno why... bull = dialog.alertDialog(self, 'Menu Item 1 Selected', 'Context Menu') def onContextMenu_Item2(self, event): bull = dialog.alertDialog(self, 'Menu Item 2 Selected', 'Context Menu') def on_TaskBarActivate(self, event): self.Iconize(False) self.Show(True) self.Raise() self.tbIcon.SetIcon(self.icon1, "PythonCard system tray sample") event.Skip() def on_TaskBarMenu(self, event): self.PopupMenu(self.contextMenu, wx.Point(-1, -1)) def on_TaskBarClose(self, event): self.reallyClose = True self.tbIcon.Destroy() self.Iconize(False) self.Show(True) self.Close() def on_minimize(self, event): self.reallyClose = False event.Skip() def on_close(self, event): if self.reallyClose: event.Skip() else: if not self.closeWarningSeen: title = 'Please Note:' txt = 'The application can only be closed by clicking\n' txt += ' the \'Quit\' button on the main window. It\n' txt += ' will now minimize to the system tray. Note\n' txt += ' also how the system tray icon changes when\n' txt += ' the app is minimized. This message will not\n' txt += ' be shown again until the program is restarted.' bull = dialog.alertDialog(self, txt, title) self.closeWarningSeen = True self.Iconize(True) self.Show(False) self.Hide() self.tbIcon.SetIcon(self.icon0, "PythonCard system tray sample") def on_bQuit_mouseClick(self, event): self.reallyClose = True self.tbIcon.Destroy() self.Close() if __name__=='__main__': app = model.Application(sysTray) app.MainLoop() --- NEW FILE: icon-on.ico --- (This appears to be a binary file; contents omitted.) --- NEW FILE: readme.txt --- PythonCard system tray integration sample. This sample shows how to use a combination of raw wxPython code and normal PythonCard code to produce an app which integrates into the system tray. It demonstrates a number of ideas: 1. The system tray icon provided has a context menu associated with it. The menu pops up when the icon is right-clicked, and items on the menu can be clicked and have their own associated event handlers. 2. When the application is minimized to the system tray, the icon is changed and will change back to the original icon when the main window is restored. 3. By intercepting the 'on_close' event in PythonCard, the application can be made to minimize to the system tray instead of shutting down. To really close the application, you have to click the 'Quit' button on the main window. |