wxPython - drawing without paint event

M

Matt Bitten

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?
 
7

7stud

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()
 
G

glenn.chappell

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?
 
B

Bjoern Schliessmann

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
 
G

glenn.chappell

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?
 
7

7stud

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()
 
B

Bjoern Schliessmann

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
 
7

7stud

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.
 
B

Bjoern Schliessmann

7stud said:
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.

Your opinion (I've seen much worse). But any suggestions what's
better for a beginner? The (incomplete) tutorial surely not.

Regards,


Björn
 
S

Steve Holden

7stud said:
Another GUI toolkit.
Well if all you have to offer is fatuous advice you would be helping
people more by keeping quiet. wxPython is a more-than-adequate GUI
toolkit, as has been proved in many successful projects.

What other toolkits have you used, and to what effect, and on what size
of program? I suspect you are not particularly well-qualified to be
answering this question in the first place, although as always you are
entitled to your opinion. It's just that you appear to have an inflated
value of its worth.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------
 
K

kyosohma

Well if all you have to offer is fatuous advice you would be helping
people more by keeping quiet. wxPython is a more-than-adequate GUI
toolkit, as has been proved in many successful projects.

What other toolkits have you used, and to what effect, and on what size
of program? I suspect you are not particularly well-qualified to be
answering this question in the first place, although as always you are
entitled to your opinion. It's just that you appear to have an inflated
value of its worth.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

If you've followed 7Stud's posts, you'll note that he/she is always
short and mildly to extremely abrupt or rude. Robin Dunn has written
on multiple occasions on the wxPython list about the trouble's they
had organizing the book with the publisher.

While the book does have issues, it is better (in my opinion) than the
only published Tkinter book, although both books are now outdated.
However, I have found that with practice, I can write a fairly complex
GUI front-end with wxPython in only a couple of hours.

As with all programming, if you don't practice, you won't learn.

Mike
 
B

Bjoern Schliessmann

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
 
K

kyosohma

Outdated to a certain limit. It's quite a bit more recent than those
many tutorials around still today using
"from wxpython.wx import *".


True.

Regards,

Björn

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
 
B

Bjoern Schliessmann

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top