我想知道何时调整了框架的大小,以便我可以保存该大小并在下次应用程序启动时记住它。这是我的 on_resize 方法:
def on_resize(self, event):
logic.config_set('main_frame_size',
(event.Size.width, event.Size.height))
event.Skip()
它是这样绑定(bind)的:
self.Bind(wx.EVT_SIZE, self.on_resize)
问题在于性能。为了安全起见,我的逻辑模块会在每次设置更改时保存配置文件,并且每次触发调整大小事件时都写入配置文件太耗费性能了。
当用户完成调整框架大小时,最好/最简单的监控方式是什么?
更新
我的config_set 函数:
def config_set(key, value):
"""Set a value to the config file."""
vprint(2, 'Setting config value: "{}": "{}"'.format(key, value))
config[key] = value
# Save the config file.
with open(config_file_path, 'w') as f:
pickle.dump(config, f)
请您参考如下方法:
你可以处理 EVT_IDLE当事件队列为空时触发:
wx.IdleEvent: This class is used for EVT_IDLE events, which are generated and sent when the application becomes idle. In other words, the when the event queue becomes empty then idle events are sent to all windows (by default) and as long as none of them call RequestMore then there are no more idle events until after the system event queue has some normal events and then becomes empty again.
调整窗口大小或移动窗口的过程应该使事件队列保持阻塞,这样在调整大小/移动完成之前它不会变空(并触发空闲事件)。
在 EVT_SIZE 中设置脏标志,并在 EVT_IDLE 处理程序中检查它。如果设置了标志,则保存新尺寸并重置标志:
import wx
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None)
self.resized = False # the dirty flag
self.Bind(wx.EVT_SIZE,self.OnSize)
self.Bind(wx.EVT_IDLE,self.OnIdle)
def OnSize(self,event):
self.resized = True # set dirty
def OnIdle(self,event):
if self.resized:
# take action if the dirty flag is set
print "New size:", self.GetSize()
self.resized = False # reset the flag
app = wx.PySimpleApp()
frame = Frame().Show()
app.MainLoop()
EVT_SIZE 也可能在恢复最小化窗口时触发(窗口大小保持不变)。如果您想减少不必要的保存,您可能需要在将其保存到配置之前检查大小是否真的不同(您可以在包含上次保存大小的变量中跟踪它)。
您可能想要添加 EVT_MOVE 以跟踪窗口位置。
