我有一个窗口,它始终位于状态显示的顶部。问题是,当它不是聚焦窗口时,文本(使用wx.StaticText)变为灰色,因此难以阅读。
即使窗口未聚焦,是否可以使文本呈现为黑色?
答案 0 :(得分:0)
您可以随时设置背景颜色,使灰色更清晰。
答案 1 :(得分:0)
最后,我创建了自己的标签小部件,禁用时不会呈灰色。不幸的是必须这样做,因为有很多样板来创建自己的小部件。如果有人想解决同样的问题,这里是小部件的代码:
class MyStaticText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label="",
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=0, validator=wx.DefaultValidator,
name="MyStaticText"):
wx.PyControl.__init__(self, parent, id, pos, size, style|wx.NO_BORDER,
validator, name)
wx.PyControl.SetLabel(self, label)
self.InheritAttributes()
self.SetInitialSize(size)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
def OnPaint(self, event):
dc = wx.BufferedPaintDC(self)
self.Draw(dc)
def Draw(self, dc):
width, height = self.GetClientSize()
if not width or not height:
return
backBrush = wx.Brush(wx.WHITE, wx.SOLID)
dc.SetBackground(backBrush)
dc.Clear()
dc.SetTextForeground(wx.BLACK)
dc.SetFont(self.GetFont())
label = self.GetLabel()
dc.DrawText(label, 0, 0)
def OnEraseBackground(self, event):
pass
def SetLabel(self, label):
wx.PyControl.SetLabel(self, label)
self.InvalidateBestSize()
self.SetSize(self.GetBestSize())
self.Refresh()
def SetFont(self, font):
wx.PyControl.SetFont(self, font)
self.InvalidateBestSize()
self.SetSize(self.GetBestSize())
self.Refresh()
def DoGetBestSize(self):
label = self.GetLabel()
font = self.GetFont()
if not font:
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
dc = wx.ClientDC(self)
dc.SetFont(font)
textWidth, textHeight = dc.GetTextExtent(label)
best = wx.Size(textWidth, textHeight)
self.CacheBestSize(best)
return best
def AcceptsFocus(self):
return False
def SetForegroundColour(self, colour):
wx.PyControl.SetForegroundColour(self, colour)
self.Refresh()
def SetBackgroundColour(self, colour):
wx.PyControl.SetBackgroundColour(self, colour)
self.Refresh()
def GetDefaultAttributes(self):
return wx.StaticText.GetClassDefaultAttributes()
def ShouldInheritColours(self):
return True