Differences between revisions 3 and 4
Revision 3 as of 2020-12-16 14:24:32
Size: 1896
Editor: Ecco
Comment:
Revision 4 as of 2020-12-16 14:36:27
Size: 4508
Editor: Ecco
Comment:
Deletions are marked like this. Additions are marked like this.
Line 51: Line 51:

"""

ZetCode wxPython tutorial.

In this example, an icon is
displayed on system tray.

Author : Jan Bodnar
Website : zetcode.com
Last modified : april 2007
Updated : Ecco december 2020
Link : https://wiki.wxpython.org/Another%20tutorial%20(Phoenix)

"""

import wx
from wx.adv import TaskBarIcon as TaskBarIcon

# class MyTaskBarIcon
# class MyFrame
# class MyApp

#---------------------------------------------------------------------------

class MyTaskBarIcon(TaskBarIcon):
    def __init__(self, frame):
        TaskBarIcon.__init__(self)

        self.frame = frame

        self.SetIcon(wx.Icon('./bitmaps/web.png', wx.BITMAP_TYPE_PNG), 'Task bar icon')

        #------------
        
        self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=1)
        self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=2)
        self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=3)

    #-----------------------------------------------------------------------
        
    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(1, 'Show')
        menu.Append(2, 'Hide')
        menu.Append(3, 'Close')

        return menu


    def OnTaskBarClose(self, event):
        self.frame.Close()


    def OnTaskBarActivate(self, event):
        if not self.frame.IsShown():
            self.frame.Show()


    def OnTaskBarDeactivate(self, event):
        if self.frame.IsShown():
            self.frame.Hide()

#---------------------------------------------------------------------------

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, (-1, -1), (290, 280))

        self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
        self.SetSize((350, 250))
        
        #------------
        
        self.tskic = MyTaskBarIcon(self)

        #------------
        
        self.Bind(wx.EVT_CLOSE, self.OnClose)

        #------------
        
        self.Centre()

    #-----------------------------------------------------------------------
        
    def OnClose(self, event):
        self.tskic.Destroy()
        self.Destroy()

#---------------------------------------------------------------------------

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'wx.adv - TaskBarIcon')
        frame.Show(True)
        self.SetTopWindow(frame)
        
        return True

#---------------------------------------------------------------------------

app = MyApp(0)
app.MainLoop()

How to create a task bar icon (Phoenix)

Keywords : Task bar icon.

Introduction :

Some applications are placed on a taskbar.

It is an area on a panel usually at the right bottom of the screen.

They are represented by a small icon.

They are specialized programs designed to do some specific task.

Common examples are a clock, sound mixer, language switcher.

They are also called applets.

In wxPython we have a TaskbarIcon class for creating such applets.

The constructor does not take any parameters.

wx.adv import TaskBarIcon as TaskBarIcon

Available methods

Destroy()

destroys a taskbaricon object

SetIcon(wx.Icon icon, string tooltip)

sets an icon with optional tooltip

IsIconInstalled()

checks if the icon is set

IsOk()

checks if the object is created

RemoveIcon()

removes the icon

PopupMenu(wx.Menu menu)

pops up a menu

(info by JanBodnar / zetcode).


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

Toggle line numbers
   1 # sample_one.py
   2 
   3 """
   4 
   5 ZetCode wxPython tutorial.
   6 
   7 In this example, an icon is
   8 displayed on system tray.
   9 
  10 Author : Jan Bodnar
  11 Website : zetcode.com
  12 Last modified : april 2007
  13 Updated : Ecco december 2020
  14 Link : https://wiki.wxpython.org/Another%20tutorial%20(Phoenix)
  15 
  16 """
  17 
  18 import wx
  19 from wx.adv import TaskBarIcon as TaskBarIcon
  20 
  21 # class MyTaskBarIcon
  22 # class MyFrame
  23 # class MyApp
  24 
  25 #---------------------------------------------------------------------------
  26 
  27 class MyTaskBarIcon(TaskBarIcon):
  28     def __init__(self, frame):
  29         TaskBarIcon.__init__(self)
  30 
  31         self.frame = frame
  32 
  33         self.SetIcon(wx.Icon('./bitmaps/web.png', wx.BITMAP_TYPE_PNG), 'Task bar icon')
  34 
  35         #------------
  36         
  37         self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=1)
  38         self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=2)
  39         self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=3)
  40 
  41     #-----------------------------------------------------------------------
  42         
  43     def CreatePopupMenu(self):
  44         menu = wx.Menu()
  45         menu.Append(1, 'Show')
  46         menu.Append(2, 'Hide')
  47         menu.Append(3, 'Close')
  48 
  49         return menu
  50 
  51 
  52     def OnTaskBarClose(self, event):
  53         self.frame.Close()
  54 
  55 
  56     def OnTaskBarActivate(self, event):
  57         if not self.frame.IsShown():
  58             self.frame.Show()
  59 
  60 
  61     def OnTaskBarDeactivate(self, event):
  62         if self.frame.IsShown():
  63             self.frame.Hide()
  64 
  65 #---------------------------------------------------------------------------
  66 
  67 class MyFrame(wx.Frame):
  68     def __init__(self, parent, id, title):
  69         wx.Frame.__init__(self, parent, id, title, (-1, -1), (290, 280))
  70 
  71         self.SetIcon(wx.Icon('./icons/icon_wxWidgets.ico', wx.BITMAP_TYPE_ICO))
  72         self.SetSize((350, 250))
  73         
  74         #------------
  75         
  76         self.tskic = MyTaskBarIcon(self)
  77 
  78         #------------
  79         
  80         self.Bind(wx.EVT_CLOSE, self.OnClose)
  81 
  82         #------------
  83         
  84         self.Centre()
  85 
  86     #-----------------------------------------------------------------------
  87         
  88     def OnClose(self, event):
  89         self.tskic.Destroy()
  90         self.Destroy()
  91 
  92 #---------------------------------------------------------------------------
  93 
  94 class MyApp(wx.App):
  95     def OnInit(self):
  96         frame = MyFrame(None, -1, 'wx.adv - TaskBarIcon')
  97         frame.Show(True)
  98         self.SetTopWindow(frame)
  99         
 100         return True
 101 
 102 #---------------------------------------------------------------------------
 103 
 104 app = MyApp(0)
 105 app.MainLoop()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Jan Bodnar (sample_one.py coding), the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

16/12/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a task bar icon (Phoenix) (last edited 2020-12-28 14:29:04 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.