wxPython框架内面板的图纸

2024-05-18 19:23:54 发布

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

下面,我有一个框架内的面板。为什么我不能画到画板上?我只看到一块纯白色的屏幕。如果我去掉面板,直接画到画框上…就行了。任何帮助都将不胜感激。在

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'window',(200,200),(600,600))
        self.Center()
        self.panel=wx.Panel(self)
        self.panel.SetBackgroundColour('white')
        self.firstpoint=wx.Point(300,300)
        self.secondpoint=wx.Point(400,400)
        self.Bind(wx.EVT_PAINT,self.onPaint)


    def onPaint(self,event):
        dc=wx.PaintDC(self.panel)
        dc.DrawLine(self.firstpoint.x,self.firstpoint.y,
                    self.secondpoint.x,self.secondpoint.y)

Tags: self框架面板initdefdcframepoint
2条回答

尝试将事件绑定到面板,而不是整个框架:

self.panel.Bind(wx.EVT_PAINT, self.onPaint)

你的版本对我来说有点用(windows),但它不断刷新面板,所以它会占用整个处理器。在

根据文档: 请注意,在paint事件处理程序中,应用程序必须始终创建wxPaintDC对象,即使您不使用它。否则,在MS Windows下,此窗口和其他窗口的刷新将出错。

在这里,您收到了整个帧的绘制事件,但对面板使用了dc。在

编辑:这个http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind很好地解释了为什么这不起作用:

^{pr2}$

在这种情况下,从不调用onPaint处理程序。在

我正在寻找一个完整的wx Python控件的完整工作示例,该控件子类wx.Panel并在其自身上进行自定义绘制,但我找不到任何一个。多亏了这个(和其他)问题,我终于找到了一个最小的工作示例——我将在这里发布,因为它确实显示了“在框架内绘制面板”;除了与OP不同的是,框架在面板上绘制图形——这里,面板自己绘制(而坐在框架中)。在

代码会产生这样的结果:

test.png

。。。基本上,当你调整窗口大小时,红色矩形会被重新绘制。在

注意代码注释,特别是OnSize中需要Refresh()以避免损坏的呈现/闪烁。在

MWE代码:

import wx

# tested on wxPython 2.8.11.0, Python 2.7.1+, Ubuntu 11.04
# http://stackoverflow.com/questions/2053268/side-effects-of-handling-evt-paint-event-in-wxpython
# http://stackoverflow.com/questions/25756896/drawing-to-panel-inside-of-frame-in-wxpython
# http://www.infinity77.net/pycon/tutorial/pyar/wxpython.html
# also, see: wx-2.8-gtk2-unicode/wx/lib/agw/buttonpanel.py

class MyPanel(wx.Panel): #(wx.PyPanel): #PyPanel also works
  def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, name="MyPanel"):
    super(MyPanel, self).__init__(parent, id, pos, size, style, name)
    self.Bind(wx.EVT_SIZE, self.OnSize)
    self.Bind(wx.EVT_PAINT, self.OnPaint)
  def OnSize(self, event):
    print("OnSize" +str(event))
    #self.SetClientRect(event.GetRect()) # no need
    self.Refresh() # MUST have this, else the rectangle gets rendered corruptly when resizing the window!
    event.Skip() # seems to reduce the ammount of OnSize and OnPaint events generated when resizing the window
  def OnPaint(self, event):
    #~ dc = wx.BufferedPaintDC(self) # works, somewhat
    dc = wx.PaintDC(self) # works
    print(dc)
    rect = self.GetClientRect()
    # "Set a red brush to draw a rectangle"
    dc.SetBrush(wx.RED_BRUSH)
    dc.DrawRectangle(10, 10, rect[2]-20, 50)
    #self.Refresh() # recurses here!


class MyFrame(wx.Frame):
  def __init__(self, parent):
    wx.Frame.__init__(self, parent, -1, "Custom Panel Demo")
    self.SetSize((300, 200))
    self.panel = MyPanel(self) #wx.Panel(self)
    self.panel.SetBackgroundColour(wx.Colour(10,10,10))
    self.panel.SetForegroundColour(wx.Colour(50,50,50))
    sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
    sizer_1.Add(self.panel, 1, wx.EXPAND | wx.ALL, 0)
    self.SetSizer(sizer_1)
    self.Layout()

app = wx.App(0)
frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()
app.MainLoop()

相关问题 更多 >

    热门问题