在 wxPython 中绘制到框架内的面板 [英] Drawing to Panel inside of Frame in wxPython
问题描述
下面,我在框架内有一个面板.为什么我无法在面板上绘图?我只是得到一个纯白色的屏幕.如果我摆脱面板并直接绘制到框架......它可以工作.任何帮助将不胜感激.
Below, I have a panel inside of a frame. Why am I not able to draw to the panel? I just get a plain white screen. If I get rid of the panel and draw directly to the frame...it works. any help would be appreciated.
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)
推荐答案
尝试将事件绑定到面板,而不是整个框架:
Try binding the event to the panel, not to the whole frame:
self.panel.Bind(wx.EVT_PAINT, self.onPaint)
你的版本对我有用(windows),但它不断重绘面板,所以它吃掉了整个处理器.
Your version kind-of work for me (windows), but it keeps redrawing the panel so it eats up the whole processor.
来自文档:请注意,在绘制事件处理程序中,应用程序必须始终创建一个 wxPaintDC 对象,即使您不使用它.否则,在 MS Windows 下,刷新此窗口和其他窗口会出错.
在这里,您收到了整个帧的绘制事件,但对面板使用了 dc.
Here, you received the paint event for the whole frame but used the dc for the panel.
这个 http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind 很好地解释了为什么这不起作用:
This http://wiki.wxpython.org/self.Bind%20vs.%20self.button.Bind explains quite nicely why this won't work:
self.Bind(wx.EVT_PAINT, self.onPaint, self.panel)
在这种情况下,永远不会调用 onPaint 处理程序.
In this case the onPaint handler is never called.
这篇关于在 wxPython 中绘制到框架内的面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!