简体   繁体   English

可以在Python中为wxMenuItem设置文本颜色吗?

[英]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. 查看wxWidgets文档 ,我看到应该只能在Windows中为wxMenuItem对象设置文本颜色。 I am using Windows, so good. 我正在使用Windows,效果很好。

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. 当使用wxPython进行编码并尝试完成此操作时,不仅没有更改菜单项的文本颜色,而且还注意到在同一菜单中该菜单项之后的菜单项缩进了1个字符。 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 wxversion.py报告我已安装2.8-msv-unicode

I played with your code and noticed that the color will only be applied if the menuitem is not yet appended to the menu. 我玩过您的代码,并注意到仅当menuitem尚未附加到菜单时才应用颜色。 So instead of menu.Append(...) , you need to to: 因此,您需要执行以下操作,而不是menu.Append(...)

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. 我在wx 2.9上使用YMMV。 I did not notice the indentation problem, but that can also be related to the version. 我没有注意到缩进问题,但这也可能与版本有关。

红色菜单项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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