无法在 Raspbian 的 wx.Panel 上设置(某些类型的)边框

Can't set (some types of) border on wx.Panel on Raspbian

我在 Raspberry Pi 上的某些文本标签上设置边框时遇到问题。在 Windows 中工作正常但在 raspbian 中失败。并非所有边界都失败。我对 wx.BORDER_RAISED 或 wx.BORDER_SUNKEN 没有问题,但对其他

这是平台问题还是我遗漏了一些 trick/setting 问题(即指定一些边框厚度或其他内容)。

这是我的示例代码

import wx

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(900, 750))
        self.SetBackgroundColour('Black')
        self.SetForegroundColour("White")

        PanelMain = wx.Panel(self, -1)
        PanelMain.SetForegroundColour("White")
        PanelMain.SetBackgroundColour("Black")
        SizerMain = wx.BoxSizer(wx.HORIZONTAL)

        for i in range(3):
            PanelSub = wx.Panel(PanelMain, -1, style=wx.BORDER_STATIC)
            PanelSub.SetBackgroundColour("Black")
            lblNew = wx.StaticText(PanelSub, -1, label="Hello {}".format(i))
            lblNew.SetForegroundColour("White")
            lblNew.SetBackgroundColour("Green" if i == 0 else "Blue")
            SizerMain.Add(PanelSub, 1, wx.EXPAND)

        PanelMain.SetSizer(SizerMain)
        SizerMain.Fit(PanelMain)

        self.Show()


app = wx.App()
Example(None, title='Boxes')
app.MainLoop()

我没有找到解决这个问题的方法,所以这就是我所做的。

首先我创建了一个自定义 wx.Panel-class 这样的

class MyCustomPanel(wx.Panel):
    def __init__(self, Parent):
        wx.Panel.__init__(self, Parent, -1)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_SIZE, self.OnSize)

    def OnSize(self, event):
        self.Refresh()
        event.Skip()

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.SetPen(wx.Pen("Gray", width=1))
        dc.SetBrush(wx.Brush("black", wx.TRANSPARENT)) 
        mySize = self.GetSize()
        dc.DrawRectangle(0,0, mySize.GetWidth(), mySize.GetHeight())

此 class 可用作普通面板,但其周围有一个框。这是主程序:

import wx
import wx.lib.stattext as ST

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(900, 750))
        self.SetBackgroundColour('Black')
        self.SetForegroundColour("White")

        PanelMain = wx.Panel(self, -1)
        PanelMain.SetForegroundColour("White")
        PanelMain.SetBackgroundColour("Black")
        SizerMain = wx.BoxSizer(wx.HORIZONTAL)

        for i in range(3):
            PanelSub = MyCustomPanel(PanelMain)
            PanelSub.SetBackgroundColour("Black")
            lblNew = ST.GenStaticText(PanelSub, -1, label="Hello {}".format(i))
            lblNew.SetForegroundColour("White")
            lblNew.SetBackgroundColour("Green" if i == 0 else "Blue")
            SizerMain.Add(PanelSub, 1, wx.EXPAND|wx.ALL, 5)

        PanelMain.SetSizer(SizerMain)
        SizerMain.Fit(PanelMain)

        self.Show()


app = wx.App()
Example(None, title='Boxes')
app.MainLoop()

注:wx.StaticText 替换为 ST.GenStaticText

注意 2: 不幸的是,在这个简单的例子中,边框被文本框覆盖了(见下图)。在我的真实代码中情况并非如此,因为我在那里使用了一个 inner sizer。在这个简单的例子中,这可以通过添加一个包含文本标签的内部 sizer 来避免。