472,102 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

wxPython - drawing without paint event

I've got a wxPython program that needs to do some drawing on a DC on a
regular basis, whether or not a paint event happens. I know how to
make a ClientDC to do the drawing in, and I know what drawing calls to
make. But how do I make it all happen? After I call MainLoop, none of
my code gets called unless there is an event. And there is no event,
so my code doesn't get called. What do I do?

Aug 10 '07 #1
12 6152
On Aug 9, 7:46 pm, Matt Bitten <mbitte...@yahoo.comwrote:
I've got a wxPython program that needs to do some drawing on a DC on a
regular basis.... And there is no event,
so my code doesn't get called. What do I do?
Then the event is: "on a regular basis", i.e. the passage of time.
You can use a wx.Timer to create events at regular intervals, which
your code can respond to:

-------
import wx

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "My Window")

panel = wx.Panel(self, -1)
self.text = wx.StaticText(panel, -1, "hello", pos=(40, 40) )

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer_event)
self.timer.Start(milliseconds=1000, oneShot=False)

def on_timer_event(self, event):
if self.text.GetLabel() == "hello":
self.text.SetLabel("goodbye")
else:
self.text.SetLabel("hello")
app = wx.App(redirect=False)

win = MyFrame()
win.Show()

app.MainLoop()
----------------

Make sure you save the timer in self so that you have a permanent
reference to the object, otherwise the timer will be destroyed when
__init_ returns.


Aug 10 '07 #2
On Aug 10, 1:40 am, 7stud <bbxx789_0...@yahoo.comwrote:
On Aug 9, 7:46 pm, Matt Bitten <mbitte...@yahoo.comwrote:
I've got a wxPython program that needs to do some drawing on a DC on a
regular basis.... And there is no event,
so my code doesn't get called. What do I do?

Then the event is: "on a regular basis", i.e. the passage of time.
You can use a wx.Timer to create events at regular intervals, which
your code can respond to:
...
Thanks!

On a related topic, it seems like it would be nice to do *all* drawing
in
response to paint events. When I get an event from the timer, I would
just tell wx that part of the window needs redrawing, and depend on it
to give me a paint even when nothing of higher priority needs to be
done.
I've seen this kind of logic recommended in other GUI tookits. Is it a
good idea in wxPython, and, if so, how does one do it?

Aug 11 '07 #3
gl************@gmail.com wrote:
On a related topic, it seems like it would be nice to do *all*
drawing in
response to paint events. When I get an event from the timer, I
would just tell wx that part of the window needs redrawing, and
depend on it to give me a paint even when nothing of higher
priority needs to be done.
I've seen this kind of logic recommended in other GUI tookits. Is
it a good idea in wxPython, and, if so, how does one do it?
At least it is not required -- there are device contexts for use
inside and outside of paint events.

One nice strategy from an example in "wxPython in Action" is the
following:

* the frame has Draw methods that draw into a BufferedDC, which is
chained to a bitmap member

* inside the paint method, the bitmap member is drawn to screen

Regards,
Björn

--
BOFH excuse #393:

Interference from the Van Allen Belt.

Aug 11 '07 #4
On Aug 11, 3:31 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
glenn.chapp...@gmail.com wrote:
On a related topic, it seems like it would be nice to do *all*
drawing in
response topaintevents. When I get aneventfrom the timer, I
would just tell wx that part of the window needs redrawing, and
depend on it to give me apainteven when nothing of higher
priority needs to be done.

One nice strategy from an example in "wxPython in Action" is the
following:

* the frame has Draw methods that draw into a BufferedDC, which is
chained to a bitmap member

* inside thepaintmethod, the bitmap member is drawn to screen
Okay, but how do I tell wx that it needs to send me a paint event?

Aug 12 '07 #5
On Aug 11, 7:59 pm, glenn.chapp...@gmail.com wrote:
On Aug 11, 3:31 am, Bjoern Schliessmann <usenet-

mail-0306.20.chr0n...@spamgourmet.comwrote:
glenn.chapp...@gmail.com wrote:
On a related topic, it seems like it would be nice to do *all*
drawing in
response topaintevents. When I get aneventfrom the timer, I
would just tell wx that part of the window needs redrawing, and
depend on it to give me apainteven when nothing of higher
priority needs to be done.
One nice strategy from an example in "wxPython in Action" is the
following:
* the frame has Draw methods that draw into a BufferedDC, which is
chained to a bitmap member
* inside thepaintmethod, the bitmap member is drawn to screen

Okay, but how do I tell wx that it needs to send me a paint event?
Try this:

--------------
import wx

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "My Window")

panel = wx.Panel(self, -1)
self.text = wx.StaticText(panel, -1, "hello", pos=(40, 40) )

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer_event)
self.timer.Start(milliseconds=1000, oneShot=False)

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

def on_timer_event(self, event):
self.GetEventHandler().ProcessEvent(wx.PaintEvent( ))

def on_paint(self, event):
print "execution entered on_paint"

app = wx.App(redirect=False)

win = MyFrame()
win.Show()

app.MainLoop()
------------

I'm not sure why the call should be
self.GetEventHandler().ProcessEvent() instead of just
self.ProcessEvent().

Aug 12 '07 #6
gl************@gmail.com wrote:
Okay, but how do I tell wx that it needs to send me a paint event?
It will send it on its own. You just need to bind your OnPaint (or
similar) method to it like 7stud demonstrates.

I really recommend reading "wxPython in Action", or at least the
tutorial.

Regards,
Björn

--
BOFH excuse #273:

The cord jumped over and hit the power switch.

Aug 12 '07 #7
On Aug 12, 6:06 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
I really recommend reading "wxPython in Action", or at least the
tutorial.
I don't. "wxPython in Action" is by far the worst computer book I've
ever purchased. It's poorly organized, poorly written, and full of
mistakes--and it's expensive. The fact that the authors foist that
piece of junk on unsuspecting newbies is a crime.

Aug 12 '07 #8
On Aug 12, 2:20 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
But any suggestions what's
better for a beginner? The (incomplete) tutorial surely not.
Another GUI toolkit.
Aug 12 '07 #9
7stud wrote:
On Aug 12, 2:20 pm, Bjoern Schliessmann <usenet-
>But any suggestions what's
better for a beginner? The (incomplete) tutorial surely not.
Another GUI toolkit.
So it seems your dislike is not for the book, but for the toolkit.

Regards,
Björn

--
BOFH excuse #414:

tachyon emissions overloading the system

Aug 12 '07 #10
ky******@gmail.com wrote:
While the book does have issues, it is better (in my opinion) than
the only published Tkinter book, although both books are now
outdated.
Outdated to a certain limit. It's quite a bit more recent than those
many tutorials around still today using
"from wxpython.wx import *".
As with all programming, if you don't practice, you won't learn.
True.

Regards,
Björn

--
BOFH excuse #103:

operators on strike due to broken coffee machine

Aug 13 '07 #11
On Aug 13, 3:15 pm, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
kyoso...@gmail.com wrote:
While the book does have issues, it is better (in my opinion) than
the only published Tkinter book, although both books are now
outdated.

Outdated to a certain limit. It's quite a bit more recent than those
many tutorials around still today using
"from wxpython.wx import *".
As with all programming, if you don't practice, you won't learn.

True.

Regards,

Björn

--
BOFH excuse #103:

operators on strike due to broken coffee machine
I guess I haven't looked up any wxPython tutorials. I typically use a
combination of the wxPython wiki, the demo and the WIA book for my
work.

Mike

Aug 13 '07 #12
ky******@gmail.com wrote:
I guess I haven't looked up any wxPython tutorials. I typically
use a combination of the wxPython wiki, the demo and the WIA book
for my work.
Me too (except the mailing list, occasionally), and it works quite
well.

Regards,
Björn

--
BOFH excuse #306:

CPU-angle has to be adjusted because of vibrations coming from the
nearby road

Aug 13 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Rene | last post: by
7 posts views Thread by Schorschi | last post: by
4 posts views Thread by Aaron Smith | last post: by
7 posts views Thread by Dennis | last post: by
1 post views Thread by edi sol | last post: by
4 posts views Thread by Sam | last post: by
1 post views Thread by ehabzaky | last post: by
4 posts views Thread by Kürþat | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.