Python:如何在wx.staticbox中绘制一些线
您好,我想在静态框内绘制一些彩色线,我使用了onpaint方法,但线没有出现在静态框内,然后我尝试创建一条静态线,这很有效,但我无法更改静态线的颜色。是否有其他方法可以使用onpaint方法或其他方法显示线条,我如何才能做到这一点 下面是一个示例代码:Python:如何在wx.staticbox中绘制一些线,python,wxpython,Python,Wxpython,您好,我想在静态框内绘制一些彩色线,我使用了onpaint方法,但线没有出现在静态框内,然后我尝试创建一条静态线,这很有效,但我无法更改静态线的颜色。是否有其他方法可以使用onpaint方法或其他方法显示线条,我如何才能做到这一点 下面是一个示例代码: import wx class MainPanel(wx.Panel): def __init__(self, parent): wx.Panel.__init__(self, parent=parent)
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()
我怀疑你的问题是你在画画之前没有设置一支有颜色的钢笔——这可能会导致在白色背景上出现一条零宽度的白线。尝试添加
self.SetBackgroundColour(wx.WHITE)
dc.Clear()
dc.SetPen(wx.Pen("BLACK", 2))
到您的OnPaint
功能
另一件需要明确的事情是,静态框是一个围绕某个对象绘制的框-您在面板上绘制,而不是在框中绘制。问题是您在面板上绘制,而不是在静态框上绘制。您可以创建一个自定义静态框并执行您喜欢的操作,然后调用原始的
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)
例如:
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()
我怎样才能把它画进盒子里?之所以有这一行,是因为如果我禁用“wx.StaticBox(self,-1,‘Example’,(5,30),size=(290185)),这一行就会出现。