In Python, WX displays the icon in the lower-right corner of the script code

Source: Internet
Author: User
Copy CodeThe code is as follows:


Import WX
Import Images
Class Demotaskbaricon (WX. Taskbaricon):
Tbmenu_restore = wx. NewId ()
Tbmenu_close = wx. NewId ()
Tbmenu_change = wx. NewId ()
Tbmenu_remove = wx. NewId ()

def __init__ (self, frame):
Wx. Taskbaricon.__init__ (self)
Self.frame = Frame

# Set the image
Icon = self. Makeicon (Images.getwxpdemoimage ())
Self. SetIcon (Icon, "WxPython Demo")
Self.imgidx = 1

# bind some events
self. Bind (WX. Evt_taskbar_left_dclick, self. ontaskbaractivate)
Self. Bind (WX. Evt_menu, self. Ontaskbaractivate, Id=self. Tbmenu_restore)
Self. Bind (WX. Evt_menu, self. Ontaskbarclose, Id=self. Tbmenu_close)
Self. Bind (WX. Evt_menu, self. Ontaskbarchange, Id=self. Tbmenu_change)
Self. Bind (WX. Evt_menu, self. Ontaskbarremove, Id=self. Tbmenu_remove)


def createpopupmenu (self):
' ""
This method is called by the base class if it needs to popup
the menu For the default Evt_right_down event. Just Create
The menu how to want it and return it from this function,
the base class takes care of the rest.
""
Menu = wx. Menu ()
menu. Append (self. Tbmenu_restore, "RESTORE wxPython Demo")
menu. Append (self. Tbmenu_close, "CLOSE wxPython Demo")
menu. Appendseparator ()
menu. Append (self. Tbmenu_change, "Change the TB Icon")
menu. Append (self. Tbmenu_remove, "REMOVE the TB Icon")
return menu


def makeicon (Self, img):
"""
The various platforms has different requirements for the
Icon size ...
"""
If "WXMSW" in WX. PlatformInfo:
img = img. Scale (16, 16)
Elif "WXGTK" in WX. PlatformInfo:
img = img. Scale (22, 22)
# Wxmac can be any size upto 128x128, so leave the source img alone ....
icon = wx. Iconfrombitmap (IMG. Converttobitmap ())
return icon

def ontaskbaractivate (self, evt):
If Self.frame.IsIconized ():
Self.frame.Iconize (False)
If not Self.frame.IsShown ():
Self.frame.Show (True)
Self.frame.Raise ()


def ontaskbarclose (self, evt):
Self.frame.Close ()


def ontaskbarchange (self, evt):
names = ["Wxpdemo", "Mondrian", "Pencil", "carrot"]
name = Names[self.imgidx]

Getfunc = getattr (Images, "get%simage"% name)
Self.imgidx + = 1
If Self.imgidx >= len (names):
SELF.IMGIDX = 0

icon = self. Makeicon (Getfunc ())
Self. SetIcon (Icon, "This is a new icon:" + name)


def ontaskbarremove (self, evt):
Self. RemoveIcon ()


Class MyFrame (WX. Frame):

Def __init__ (self):
WX. Frame.__init__ (self, None,-1, "My Frame", size= ())
Panel = wx. Panel (self,-1)
Panel. Bind (WX. Evt_motion, self. OnMove)
WX. Statictext (Panel,-1, "Pos:", pos= (Ten))
Self.posctrl = wx. Textctrl (Panel,-1, "", pos= (+))

Try:
Self.tbicon = Demotaskbaricon (self)
except:
Self.tbicon = None

#wx. Callafter (self. Showtip)

#self. Bind (WX. Evt_close, self. Onclosewindow)
#self. Bind (WX. Evt_iconize, self. oniconfiy)
def onclosewindow (Self, event):
self.dying = True
Self.demopage = none
Self.codepage = None
Self.mainmenu = None
If Self.tbicon is not None:
Self.tbicon.Destroy ()
Self. Destroy ()
def oniconfiy (self, evt):
WX. LogMessage ("oniconfiy:%s"% evt. Iconized ())
evt. Skip ()
def OnMove (Self, event):
pos = event. GetPosition ()
Self.posCtrl.SetValue ("%s,%s"% (pos.x, pos.y))

if __name__ = = ' __main__ ':
App = WX. Pysimpleapp ()
frame = MyFrame ()
Frame. Show (True)
App. Mainloop ()

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.