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

2024-09-30 03:22:18 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的树莓皮上的一些文本标签上设置边框有问题。在Windows下工作正常,但在raspbian上失败。并不是所有的边界都会失败。我对wx.BORDER\u升高或wx.BORDER\u下沉没有问题,但其他的

这是一个平台的问题,还是有一些技巧/设置,我遗漏了(即指定一些边界厚度或东西)

这是我的示例代码

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

Tags: selftitleinitexample边界blackwxwhite
1条回答
网友
1楼 · 发布于 2024-09-30 03:22:18

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

首先,我创建了一个自定义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.GenStaticTextas this works better on GTK

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

Textbox overlaps the border of the panel

相关问题 更多 >

    热门问题