I'm still trying to figure out a way to do regression testing with
wxPython. I have figured out how to programmatically control certain
widgets like buttons, but I am not struggling with menu items.
Below is a piece of code where I am able to programmatically click on a
button with clickOnButton(), but the similar function clickMenuItem()
does not seem to work.
When I run this script, I get:
Button was clicked
i.e., the message from OnHiMenu() is not printed. Why is the message not
processed by the menu bar object?
Thx.
== Sample code:
import wx
class MyFrame(wx.Frame):
def __init__(self, parent, ID, title):
wx.Frame.__init__(self, parent, ID, title,
wx.DefaultPosition, wx.Size(200, 150))
self.ID_HI = wx.NewId()
menu = wx.Menu()
menu.Append(self.ID_HI, "&Hi", "Say hi")
self.menuBar = wx.MenuBar()
self.menuBar.Append(menu, "&File");
self.SetMenuBar(self.menuBar)
wx.EVT_MENU(self, self.ID_HI, self.OnHiMenu)
self.button = wx.Button(self, wx.NewId(), "Button",
wx.DefaultPosition,
wx.DefaultSize)
wx.EVT_BUTTON(self, self.button.GetId(), self.OnButton)
def OnHiMenu(self, event):
print "Hi menu item was clicked"
def OnButton(self, event):
print "button was clicked"
class MyApp(wx.App):
def OnInit(self):
self.frame = MyFrame(None, -1, "Hello from wxPython")
self.frame.Show(True)
self.SetTopWindow(self.frame)
return True
def clickOnButton(button):
clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED,
button.GetId())
button.ProcessEvent(clickEvent)
def clickMenuItem(menuBar, itemID):
menuItem = menuBar.FindItemById(itemID)
clickEvent = wx.CommandEvent(wx.wxEVT_COMMAND_MENU_SELECTED, itemID)
# Doesn't compile. MenuItem does not have a ProcessEvent method
# menuItem.ProcessEvent(clickEvent)
# Compiles, but menu item does not seem to be picked.
menuBar.ProcessEvent(clickEvent)
from WxManipulators import *
if __name__ == "__main__":
app = MyApp(0)
clickOnButton(app.frame.button)
clickMenuItem(app.frame.menuBar, app.frame.ID_HI)