2013-06-26 100 views
0

我一直在嘗試最小化應用程序到托盤,並且一旦它返回到屏幕,我遇到問題。最小化到托盤應用程序後未正確關閉

我的應用程序正常關閉,只要我不會最小化,一旦我最小化我的OnExit()不執行。

這是我的代碼;

import wx 

class simpleapp_wx(wx.Frame): 
    def __init__(self,parent,id,title): 
     wx.Frame.__init__(self,parent,id,title, pos = wx.DefaultPosition, size = wx.Size(-1,-1)) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     Main_Frame = wx.BoxSizer(wx.VERTICAL) # Creates the Main box Sizer Parent 
     self.icon = wx.Icon('MainIcon.ico', wx.BITMAP_TYPE_ICO) # This gets an Icon for the application 
     self.SetIcon(self.icon) # This assigns the Icon 

     self.Bind(wx.EVT_CLOSE, self.close_window) # This closes the window 

     self.tbicon = wx.TaskBarIcon() # This assigns the Icon control that will be used when minimixed to tray 
     self.Bind(wx.EVT_ICONIZE, self.OnIconify) # This calls the function that minimizes to tray (Iconize = Minimize) 
     self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate) # This is what return the application to the screen. TaskBar Left Double Click 

     BottomButtons = wx.StdDialogButtonSizer() 
     self.BottomButtonsClose = wx.Button(self, wx.ID_CLOSE, "Close") 
     BottomButtons.AddButton(self.BottomButtonsClose) 
     BottomButtons.Realize(); 

     self.BottomButtonsClose.Bind(wx.EVT_BUTTON, self.close_window) # Button click event handler 

     Main_Frame.Add(BottomButtons, -1, wx.EXPAND, 5) 

     self.SetSizerAndFit(Main_Frame) # makes the Parent window size to all the items on screen 
     self.Layout() 

    def close_window (self, event): 
     self.Destroy() # Destroy the box on close 
     print "Destroy" 

    def OnTaskBarActivate(self, evt): # Return from the Taskbar 
     if self.IsIconized(): 
      print "Return to Front" 
      self.Iconize(False) # Hide the icon from the Taskbar 
      self.Show() # Show the Main Window 
      self.Raise() #Raise the Main Window to the screen 
      self.tbicon.RemoveIcon() # Remove the Icon from the Taskbar 

    def OnIconify(self, evt): 
     if evt.Iconized(): 
      print "Minimize to Tray" 
      self.Iconize(True) # Show the Icon on the Taskbar 
      self.Hide() # Hide the Main Window from the screen 
      self.tbicon.SetIcon(self.icon) #Set the Icon on the Taskbar 
    ''' ------------------------------------------------------------------------------------------------------------ 
    Bottom of the Button and Control Definitions; 
    -------------------------------------------------------------------------------------------------------------''' 

class MyApp(wx.App): 
    def OnInit(self): 
     frame = simpleapp_wx(None, -1, "Minimize to Tray") # Call/assign the Main Frame 
     frame.Show(True) # Show the Frame 
     frame.Centre() # Center it on the screen 
     return True 
    def OnExit(self): 
     print "OnExit Destroy0" 
     self.Destroy() 
     print "OnExit Destroy1" 
     return False 

app = MyApp(0) 
app.MainLoop() 

我只在Python/wxPython的編碼,現在約一個月,這是我在試圖最小化到托盤第一次嘗試。我很高興,直到我的應用程序沒有正常關閉。我在使用IsIconize時看過很多不同的例子,但我沒有看到我做錯了什麼。

感謝您的期待。

Vin

回答

0

您需要在銷燬幀之前刪除tbicon對象。我第一次使用它們花了我一段時間才弄清楚。把下面的東西放到你的關閉處理程序中:

self.tbIcon.RemoveIcon() 
self.tbIcon.Destroy() 
self.Destroy() 

你會注意到我刪除了圖標,然後將其銷燬。我知道當我用2.8來做這件事的時候,我必須回來,但是可能2.9不再需要tbicon.Destroy()。你必須玩它才能看到。

您還可以閱讀我的教程就這個問題,如果你喜歡:http://www.blog.pythonlibrary.org/2011/12/13/wxpython-101-creating-taskbar-icons/

+0

@邁克·德里斯科爾 - 謝謝你,我已經使用.RemoveIcon()試過了,但即使沒有想到.Destroy的()。現在工作正常。 – Vinster411

相關問題