I have been learning the implementation of the system tray, so I wrote a simple system tray instance, right-click the instance, including demo, maximize, minimize, exit, and about. The test passed in python2.6.
# -*-Coding: cp936 -*-
Import WX
Class Taskbaricon (wx. taskbaricon ):
Id_play = wx. newid ()
Id_about = wx. newid ()
Id_minshow = wx. newid ()
Id_maxshow = wx. newid ()
Id_closeshow = wx. newid ()
Def _ Init __ (Self, frame ):
Wx. taskbaricon. _ Init __ (Self)
Self. Frame = frame
Self. seticon (wx. Icon (name = ' Wx. ICO ' , Type = wx. bitmap_type_ico ), ' System Tray demonstration! ' ) # Wx. ICO is the ICO icon file
Self. BIND (wx. evt_taskbar_left_dclick, self. ontaskbarleftdclick) # Double-click
Self. BIND (wx. evt_menu, self. onplay, id = self. id_play)
Self. BIND (wx. evt_menu, self. onabout, id = self. id_about)
Self. BIND (wx. evt_menu, self. onminshow, id = self. id_minshow)
Self. BIND (wx. evt_menu, self. onmaxshow, id = self. id_maxshow)
Self. BIND (wx. evt_menu, self. oncloseshow, id = self. id_closeshow)
Def Ontaskbarleftdclick (self, event ):
If Self. Frame. isiconized ():
Self. Frame. iconize (false)
If Not Self. Frame. isshown ():
Self. Frame. Show (true)
Self. Frame. Raise ()
Def Onplay (self, event ):
Wx. MessageBox ( ' Python implementationProgramA demonstration of the system tray, which runs on python2.6! ' , ' Demo ' )
Def Onabout (self, event ):
Wx. MessageBox ( ' System Tray demonstration V1.0 python2.x! ' , ' About ' )
Def Onminshow (self, event ):
Self. Frame. iconize (true)
Def Onmaxshow (self, event ):
If Self. Frame. isiconized ():
Self. Frame. iconize (false)
If Not Self. Frame. isshown ():
Self. Frame. Show (true)
Self. Frame. Raise ()
Self. Frame. Maximize (true) # Maximize display
Def Oncloseshow (self, event ):
Self. Frame. Close (true)
# Right-click menu
Def Createpopupmenu (Self ):
Menu = wx. menu ()
Menu. append (self. id_play, ' Demo ' )
Menu. append (self. id_minshow, ' Minimize ' )
Menu. append (self. id_maxshow, ' Maximize ' )
Menu. append (self. id_about, ' About ' )
Menu. append (self. id_closeshow, ' Exit ' )
Return Menu
Class Frame (wx. Frame ):
Def _ Init __ (
Self, parent = none, id = wx. id_any, Title = ' Taskbaricon ' , Pos = wx. defaultposition,
Size = wx. defaultsize, style = wx. default_frame_style
):
Wx. Frame. _ Init __ (Self, parent, ID, title, POs, size, style)
Self. seticon (wx. Icon ( ' Wx. ICO ' , Wx. bitmap_type_ico ))
Panel = wx. Panel (self, wx. id_any)
Statictext = wx. statictext (panel, wx. id_any, label = u ' Hello World ' , Pos = wx. Point (88,152), size = wx. Size (200,100 ))
Statictext. setfont (wx. Font (26, wx. Swiss, wx. Normal, wx. Normal, false, u ' Tahoma ' ))
Statictext. setforegroundcolour (wx. colour (255, 0, 0 ))
Sizer = wx. boxsizer ()
Sizer. Add (statictext,-1, wx. Top | wx. Left, 200)
Panel. setsizer (Sizer)
Self. taskbaricon = taskbaricon (Self)
# Bind event
Self. BIND (wx. evt_close, self. onclose)
Self. BIND (wx. evt_iconize, self. oniconfiy) # When the window is minimized, oniconfiy is called. Pay attention to the minimization button on the Wx form. The event triggered is wx. evt_iconize, and there is no wx defined at all. evt_minimize, but to maximize, there is a wx. evt_maximize.
Def Onhide (self, event ):
Self. Hide ()
Def Oniconfiy (self, event ):
Self. Hide ()
Event. Skip ()
Def Onclose (self, event ):
Self. taskbaricon. Destroy ()
Self. Destroy ()
Def Testframe ():
APP = wx. pysimpleapp ()
Frame = frame (size = (640,480 ))
Frame. centre ()
Frame. Show ()
App. mainloop ()
If _ Name __ = ' _ Main __ ' :
Testframe ()