如何在 wxpython 中发送 PaintEvent [英] How to send PaintEvent in wxpython

查看:52
本文介绍了如何在 wxpython 中发送 PaintEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 wxpython 中生成 PaintEvent 以触发重新绘制窗口.但是,我似乎无法使其工作,因为我尝试在本机绘制事件之外使用 PaintDC,因此出现错误.

I'm trying to generate a PaintEvent in wxpython to trigger redrawing a window. However, I can't seem to make it work, I get errors because I try to use a PaintDC outside of a native paint event.

这是一个最小的例子:

import wx

class AppFrame(wx.Frame):
    def __init__(self):
        super(AppFrame, self).__init__(parent=None, title="Demo")
        self.SetClientSize((800,600))

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

        self.Bind(wx.EVT_LEFT_DOWN, self.onclick)

    def onclick(self, event):
        wx.PostEvent(self,wx.PaintEvent())

    def paint(self, event=None):
        print "paint"
        dc = wx.PaintDC(self)
        dc.SetPen(wx.Pen(wx.BLACK, 4))
        dc.DrawLine(0, 0, 50, 50)

if __name__ == "__main__":
    app = wx.App(redirect=False)
    appFrame = AppFrame()
    appFrame.Show()
    app.MainLoop()

我知道我可以在 onclick 处理程序中调用 Refresh() 来获得相同的功能(并使用 CallAfter 使其线程安全),但我想了解为什么我不能发送 PaintEvents.

I know I could call Refresh() in the onclick handler to get the same functionality (and use CallAfter to make it threadsafe), but I would like to understand why I can't send PaintEvents.

我在 Mac OS 10.7 上使用 wxpython 3.0.1.1,Python 2.7.1.我得到的错误是

I'm on Mac OS 10.7 with wxpython 3.0.1.1, Python 2.7.1. The error I get is

  File "test.py", line 17, in paint
    dc = wx.PaintDC(self)
  File "/usr/local/lib/wxPython-3.0.0.0/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_gdi.py", line 5122, in __init__
    _gdi_.PaintDC_swiginit(self,_gdi_.new_PaintDC(*args, **kwargs))
wx._core.PyAssertionError: C++ assertion "window->MacGetCGContextRef() != NULL" failed at /BUILD/wxPython-src-3.0.0.0/src/osx/carbon/dcclient.cpp(195) in wxPaintDCImpl(): using wxPaintDC without being in a native paint event

推荐答案

调用窗口的 Refresh 方法将触发绘制事件,将窗口或其子矩形标记为损坏".然后系统会尽快发送一个绘制事件,所有损坏"的区域都添加到事件的更新区域中.如果您希望绘制事件立即发生而不是等待系统,那么您可以调用窗口的 Update 方法,但是通常正确的做法是等待系统自然发送它.

Calling the window's Refresh method will trigger a paint event, by marking the window or a subrectangle of it as "damaged". The system will then send a paint event as soon as it can, with all of the "damaged" regions added together in the event's update region. If you want the paint event to happen immediately instead of waiting for the system you can then call the window's Update method, however usually the right thing to do is to just wait for the system to send it naturally.

这篇关于如何在 wxpython 中发送 PaintEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆