#!/usr/bin/env python
# vim: tabstop=8 shiftwidth=4 softtabstop=4
# python version: 2.7.5 final, serial: 0
import sys
import wxversion
wxversion.select('2.8')
import wx
class DemoApp(wx.App):
def OnInit(self):
self._frame = DemoMainFrame(None, title='Send Event Demo')
self.SetTopWindow(self._frame)
self._frame.Show()
return True
# end of OnInit
# end of DemoApp
class DemoMainFrame(wx.Frame):
def __init__(self, parent, id_=wx.ID_ANY, title='Main', \
pos=wx.DefaultPosition, size=wx.DefaultSize, \
style=wx.DEFAULT_FRAME_STYLE, name='Main'):
super(DemoMainFrame, self).__init__(\
parent, id_, title, pos, size, style, name)
self.Centre(wx.BOTH)
self._mainMenu = wx.MenuBar()
self._fileMenu = wx.Menu()
self._checkItem = self._fileMenu.AppendCheckItem(\
wx.ID_ANY, 'Check Item')
self.Bind(wx.EVT_MENU, self._onCheckItemClick, self._checkItem)
self._mainMenu.Append(self._fileMenu, 'File(&F)')
self.SetMenuBar(self._mainMenu)
self._panel = wx.Panel(self, wx.ID_ANY)
self._button = wx.Button(self._panel, wx.ID_ANY, 'Send Event')
self._button.SetPosition((10, 10))
self._button.Bind(wx.EVT_BUTTON, self._onButtonClick)
# end of __init__
def _onCheckItemClick(self, event):
style = wx.OK | wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.CENTRE
dialog = wx.MessageDialog(self, 'Check Item Clicked!', 'Check Item', style)
dialog.ShowModal()
if dialog:
dialog.Destroy()
# end of _onCheckItemClick
def _onButtonClick(self, event):
sendEvent(wx.wxEVT_COMMAND_MENU_SELECTED, self._checkItem.Id, \
self._fileMenu, self.GetEventHandler())
self._checkItem.Check(not self._checkItem.IsChecked())
# end of _onButtonClick
def sendEvent(eventType, targetID, eventObj, eventHandler, isSetInt=True, \
eventClass=wx.CommandEvent):
"""Send a event manually."""
event = eventClass(eventType, targetID)
if isSetInt:
event.SetInt(1)
event.SetEventObject(eventObj)
#wx.PostEvent(eventHandler, event)
assert eventHandler.ProcessEvent(event), 'Send event fail!'
# end of sendEvent
def _main():
try:
app = DemoApp(False)
app.SetAppName('Send Event Demo')
app.MainLoop()
except KeyboardInterrupt:
sys.exit(0)
# end of _main
if __name__ == '__main__':
_main()
# end of __main__
沒有留言:
張貼留言