如图,panel或控件获得焦点时,增加阴影边框和背景变黑,过两秒再变回来,如此往复
改变Panel的边框有点困难,改变背景倒是简单。下面的代码生成了两个panel,获得焦点后就会变色,2秒钟后复原。
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1)
self.SetTitle('Panel焦点事件')
self.SetBackgroundColour((224, 224, 224))
self.SetSize((300, 100))
self.Center()
p1 = wx.Panel(self, -1, style=wx.SUNKEN_BORDER)
p1.SetBackgroundColour((224, 224, 224))
p2 = wx.Panel(self, -1, style=wx.RAISED_BORDER)
p2.SetBackgroundColour((224, 224, 224))
bs = wx.BoxSizer()
bs.Add(p1, 1, wx.EXPAND|wx.ALL, 5)
bs.Add(p2, 1, wx.EXPAND|wx.ALL, 5)
self.SetSizer(bs)
self.Layout()
self.currut_panel = None
self.timer = wx.Timer()
self.timer.Bind(wx.EVT_TIMER, self.on_timer)
p1.Bind(wx.EVT_SET_FOCUS, self.on_focus)
p2.Bind(wx.EVT_SET_FOCUS, self.on_focus)
def on_focus(self, evt):
self.currut_panel = evt.GetEventObject()
self.currut_panel.SetBackgroundColour((255, 224, 224))
self.Refresh()
self.timer.StartOnce(2000)
def on_timer(self, evt):
self.currut_panel.SetBackgroundColour((224, 224, 224))
self.Refresh()
if __name__ == '__main__':
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()