user2971990
user2971990

Reputation: 127

wx.Python: How can I draw some lines into wx.staticbox

Hello I want to draw some colored lines inside a staticbox,I used the onpaint method but the line didn't appear inside the static box, then I tried to create a static line,this worked but I can't change the colour of the static line. Is any other way to show the line with the onpaint method or something else, and how can I do that?

Here is an example code:

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

Upvotes: 3

Views: 1801

Answers (2)

user1129665
user1129665

Reputation:

The thing is that you are painting on the panel rather then the static box. You can create a custom static box and do what you like and then call the original wx.StaticBox.OnPaint like:

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

for example:

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

Upvotes: 2

Steve Barnes
Steve Barnes

Reputation: 28405

I suspect that your problem is that you are not setting a pen up with a colour before drawing - this is likely to result in a white line of zero width on a white background. Try adding

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

To your OnPaint function.

The other thing to be clear on is that a static box is a box draw round something - you draw on the panel not into the box.

Upvotes: 0

Related Questions