andrea valle wrote:
The idea was: press the button, clear the drawing space and generate a new rectangle with random position.
OK, got it.
I changed PaintDC with ClientDC but it seems I didn't solve the problem.
If I put the line "self.dc = wx.ClientDC(panel)" in the __init__ of the Button I obtain nothing,
you don't want to store a DC like that anyway, you need to create it when you need it.
if I put it in the OnPaint def I obtain the
same result as before.
I suppose there's (also) a design error.
Yes, that's your core problem. You don't' need a custom button class, you need a custom wx.Panel(or a wx.Window would do), to do the drawing in, and a custom wx.Frame to put the button and Panel on.
I've enclosed a version that I think does what you want. I totally re-structured it, and I took the liberty of laying things out with a Sizer, I NEVER use explicit positioning.
Make sure you look in the Wiki at:
http://wiki.wxpython.org/index.cgi/RecipesImagesAndGraphics
(and everywhere else, there's a lot of info in there)
Also, if you need to draw stuff like this, and don't want to deal with all the wx.PaintDCs and all, check out the FloatCanvas, it's in the demo in recent releases.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov
#!/usr/bin/python2.3
import fixdc
import wx
from random import *
class MyPanel(wx.Panel):
def __init__(self, parent, size, position):
wx.Panel.__init__(self, parent, -1, size=size, pos=position)
self.NewRect()
self.Bind(wx.EVT_PAINT, self.OnPaint)
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.Clear()
dc.DrawRectangle(*self.rect)
def NewRect(self):
self.rect = (randint(0, 300), randint(0, 300), 50, 50)
def DrawRect(self):
self.NewRect()
dc = wx.ClientDC(self)
dc.Clear()
dc.DrawRectangle(*self.rect)
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,"test", size = (500, 500))
button = wx.Button(self, -1, label="butt")
button.Bind(wx.EVT_BUTTON, self.UpdatePanel)
self.Panel = MyPanel(self, (200,200), (100,250))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(button,0,wx.ALIGN_CENTER | wx.ALL, 10)
sizer.Add(self.Panel,1,wx.EXPAND)
self.SetSizer(sizer)
def UpdatePanel(self,event):
self.Panel.DrawRect()
if __name__ == '__main__':
app = wx.App(0)
frame = MyFrame()
frame.Show()
app.MainLoop()
---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org