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

问题描述 投票:0回答:1

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

这是一个平台问题还是我缺少一些技巧/设置(即指定一些边框厚度或某些东西)。

这是我的示例代码

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()
python wxpython
1个回答
0
投票

我没有找到解决方案,所以这就是我所做的。

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

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())

此类可用作普通面板,但周围有一个框。这是主要计划:

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替换as this works better on GTK

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

Textbox overlaps the border of the panel

© www.soinside.com 2019 - 2024. All rights reserved.