سؤال

I'm aware that there are literally hundred of examples out there for this task, though I don't manage to apply those examples to my specific problem. As you can see on the code below I am trying to draw a polygon on a bitmap "self.image". This code works absolutely fine on MS Windows. On Linux this code will not draw my polygon.

I tried to play around with different "dcs" like MemoryDC according to this How to draw text in a bitmap using wxpython? but the result was the same.

My questions are: Why does my code fail on Linux? Why does this work on MS Windows? (a bit off-topic) Why are people often exclusively drawing in the PaintDC in the OnPaint method bound to EVT_PAINT?

class attributes:

    self.dc = wx.ClientDC(self.image)
    self.dc.SetPen(wx.Pen(colour='red', width=4, style=wx.SOLID))
    self.polygon = list()

this method is being called when I want to start drawing:

def start_drawing(self):
    self.image.Bind(event=wx.EVT_LEFT_DOWN, handler=self.draw_polygon)
    self.dc.BeginDrawing()

this method handles the binding from above:

def draw_polygon(self, event):
    self.polygon.append(event.GetPositionTuple())
    if len(self.polygon) > 1:
        self.dc.DrawLines(points=self.polygon)
هل كانت مفيدة؟

المحلول

I fixed the bug myself. The problem was actually not visible in the code I've been providing.

I have a method which changes self.image before any drawing is made. Thus the image which I assign to the ClientDC in the class body is not the same image than self.image when I begin to draw. Adding self.dc = wx.ClientDC(self.image) in the image setter solved my problem.

I don't understand why this code worked when being executed on MS Windows. This should not have never worked in the first place.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top