使用 wxPython 构建的应用程序在最小化到托盘后,无法正确关闭。当应用程序运行时,如果用户点击关闭按钮或调用了 wx.Frame.Destroy() 方法,应用程序会正常关闭。但是,如果用户将应用程序最小化到托盘,然后双击托盘图标将其还原,应用程序不会关闭。
2、解决方案
要解决此问题,需要在应用程序中添加一些额外的代码来处理托盘图标的双击事件。在 wxPython 中,可以使用 wx.TaskBarIcon.Bind() 方法来绑定托盘图标的双击事件。当托盘图标被双击时,应用程序应该调用 wx.Frame.Show() 方法来显示主窗口,并调用 wx.Frame.Raise() 方法将主窗口提升到最前面。
以下是修改后的代码示例:
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()
在上面的代码中,我们添加了 self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarActivate) 来绑定托盘图标的双击事件,并添加了 self.OnTaskBarActivate() 方法来处理托盘图标的双击事件。在 self.OnTaskBarActivate() 方法中,我们调用了 self.Show() 和 self.Raise() 方法来显示主窗口并将其提升到最前面。这样,当用户双击托盘图标时,应用程序的主窗口就会显示出来,并且应用程序就不会关闭了。
希望这个解决方案对您有所帮助!