Hi everybody,
I was hoping you could help me with my current problem: drawing on a button.
Basically I’d like to use a device context to draw on a simple wxPython bitmapped button (be it lines, ellipses, rectangles - you name it).
Here’s what I’ve tried first:
------------------------------------------------ Begin code ----------------------------------------------------
#!/usr/bin/env python
import wx
class MyFrame(wx.Frame):
def init(self, parent = None, id = -1, pos = (10,10), title = “HelloWorld”, size = (800,600)):
wx.Frame.init(self, parent,id,title, pos, size)
self.CreateStatusBar()
self.pnl = wx.Panel(self)
self.pnl.SetBackgroundColour(“blue”)
self.btn1 = wx.BitmapButton(self.pnl, pos = (50,50), size=(50,50))
dc = wx.ClientDC(self)
dc.SetBrush(wx.Brush(“black”))
dc.SetPen(wx.Pen(“black”,1))
dc.DrawLine(1,1,200,200)
class App(wx.App):
def OnInit(self):
self.frame = MyFrame()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App(False)
app.MainLoop()
if name == ‘main’:
main()
------------------------------------------------ End code ----------------------------------------------------
This doesn’t work - it doesn’t draw anything. I’m assuming it’s because it DOES actually draw something, but whatever IS drawn gets quickly overwritten by some
“maintenance” paint event of the button. Is this true?
With this in mind, I tried creating a buffered DC and drawing to the button upon catching an EVT_PAINT event. This is the code I’ve changed in my
init method:
------------------------------------------------ Begin code ----------------------------------------------------
def init(self, parent = None, id = -1, pos = (10,10), title = “HelloWorld”, size = (800,600)):
wx.Frame.init(self, parent,id,title, pos, size)
self.CreateStatusBar()
self.pnl = wx.Panel(self)
self.pnl.SetBackgroundColour(“blue”)
self.btn1 = wx.BitmapButton(self.pnl, pos = (50,50), size=(50,50))
self.btn1.Bind(wx.EVT_PAINT, self.PaintButton)
self.buffer=wx.EmptyBitmap(100,100)
dc=wx.BufferedDC(None, self.buffer)
dc.Clear()
dc.SetBrush(wx.Brush(“black”))
dc.SetPen(wx.Pen(“black”,1))
dc.DrawRectangle(10,10,100,100)
def PaintButton(self, event):
dc = wx.BufferedPaintDC(self, self.buffer)
------------------------------------------------ End code ----------------------------------------------------
Not only does this not work, it also completely brings the program to a halt - probably trying to draw over and over again.
When I bind the EVT_PAINT to the panel in which the button is contained, the program doesn’t come to a halt, but still nothing is drawn.
So, my questions are (in addition to any guidance you can offer on the matter of drawing on buttons):
1.) Why doesn’t program #1 work?
2.) This is a bit off-topic, but I’ll ask it anyway: why does program #2 become hopelessly stuck when I bind the EVT_PAINT to the button? And why doesn’t it when I bind it to the panel?
3.) Why doesn’t program #2 (with self.Bind instead of self.btn1.Bind) work? Isn’t “catching” the EVT_PAINT enough?
Your help is most kindly appreciated :).
Assaf.