简体   繁体   中英

Can one set the text colour for a wxMenuItem in Python?

Looking at the wxWidgets documentation , I see that one should be able to set the text colour for a wxMenuItem object in Windows only. I am using Windows, so good.

When coding in wxPython and trying to accomplish this, not only do I not get the menu item's text colour changed, but I notice that the menu item which follows this menu item in the same menu gets indented by 1 character. Very strange indeed. Should I remove the directive to set the text colour, the two menu items line up as expected.

So here is my code. I don't see any mistakes in my code, but perhaps there is something because I am sure that indentation is a sign something is up.

menu = wx.Menu()
colour = (255,0,0) # like the text to be red
m_cluster = menu.Append(-1, "&Cluster\tAlt-C", "Cluster Options.")
m_cluster.SetTextColour(colour) # remembered to spell color with u
self.Bind(wx.EVT_MENU, self.OpenClusterDialog, m_cluster)
m_data = menu.Append(-1, "Data Source", "Set Data Source Information")
self.Bind(ex.EVT_MENU, self.OpenDataSourceDialog, m_data)
menuBar.Append(menu, "&Options") # menu bar previously defined

wxversion.py reports that I have 2.8-msv-unicode installed

I played with your code and noticed that the color will only be applied if the menuitem is not yet appended to the menu. So instead of menu.Append(...) , you need to to:

m_cluster = wx.MenuItem(menu, -1, "&Cluster\tAlt-C", "Cluster Options.")
m_cluster.SetTextColour(colour)
menu.AppendItem(m_cluster)

I'm on wx 2.9 so YMMV. I did not notice the indentation problem, but that can also be related to the version.

红色菜单项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM