wx.Python:如何在wx.staticbox中绘制一些线条

时间:2013-12-03 13:13:13

标签: python wxpython

您好我想在静态框内绘制一些彩色线条,我使用的是onpaint方法,但是线条没有出现在静态框内,然后我试图创建一个静态线,这个工作但我无法改变静态线的颜色。是否有其他方式用onpaint方法或其他方法显示该行,我该怎么做?

以下是一个示例代码:

import wx

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.frame = parent

        wx.StaticBox(self, -1, 'Example', (5, 30), size=(290, 185))

        self.Bind(wx.EVT_PAINT, self.OnPaint)

        #self.line = wx.StaticLine(self,-1, (25, 150), (100,1))
        #self.line.SetForegroundColour(("blue"))

    def OnPaint(self, event):
        dc = wx.PaintDC(self)
        dc.DrawLine(50, 10, 80, 10)
        dc.DrawLine(50, 140, 80, 140)
        dc.DrawLine(50, 300, 80, 300)

class MainFrame(wx.Frame): 

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Window",size=(305,430))
        panel = MainPanel(self)
        self.CenterOnParent()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

2 个答案:

答案 0 :(得分:2)

问题是你在面板上绘画而不是静态框。您可以创建自定义静态框并执行您喜欢的操作,然后调用原始wx.StaticBox.OnPaint,如:

# custom StaticBox class
class MyStaticBox(wx.StaticBox):
  def __init__(self, *args, **kwargs):
    super(MyStaticBox, self).__init__(*args, **kwargs)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

  def OnPaint(self, event):
    # do what you want here
    width, height = self.GetSize()
    dc = wx.PaintDC(self)
    dc.Clear()
    dc.SetPen(wx.Pen('#4285F4'))
    dc.DrawLine(0, 0, width, height)
    dc.DrawLine(width, 0, 0, height)
    # after you finished, call the StaticBox OnPaint
    super(MyStaticBox, self).OnPaint(event)

例如:

import wx

# custom StaticBox class
class MyStaticBox(wx.StaticBox):
  def __init__(self, *args, **kwargs):
    super(MyStaticBox, self).__init__(*args, **kwargs)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

  def OnPaint(self, event):
    # do what you want here
    width, height = self.GetSize()
    dc = wx.PaintDC(self)
    dc.Clear()
    dc.SetPen(wx.Pen('#4285F4'))
    dc.DrawLine(0, 0, width, height)
    dc.DrawLine(width, 0, 0, height)
    # after you finished, call the StaticBox OnPaint
    super(MyStaticBox, self).OnPaint(event)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.frame = parent

        MyStaticBox(self, -1, 'Example', (5, 30), size=(290, 185))

        #self.line = wx.StaticLine(self,-1, (25, 150), (100,1))
        #self.line.SetForegroundColour(("blue"))

class MainFrame(wx.Frame): 

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Window",size=(305,430))
        panel = MainPanel(self)
        self.CenterOnParent()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

Example

答案 1 :(得分:0)

我怀疑你的问题是你在绘图之前没有用彩色设置笔 - 这可能会在白色背景上产生零宽度的白线。尝试添加

self.SetBackgroundColour(wx.WHITE)
dc.Clear()
dc.SetPen(wx.Pen("BLACK", 2))

OnPaint功能。

另一件需要明确的事情是,静态框是一个圆形框画 - 你在面板上画画而不是框。