2012-08-02 50 views
2

嘿我試圖在用戶單擊圖上的一個點時在圖像頂部繪製一個矩形。wxpython在StaticBitmap上繪圖

要滾動不同的圖表,我使用了一個staticBitmap。不幸的是,幾乎每一次與區議會的嘗試都不成功。 PaintDC和BufferedDC都會導致無限循環發生,有時會將圖形放在圖像後面。 ClientDC顯示我繪製的框,但當我調整大小時,它會消失。當我僅將圖像保存到文件中時,使用MemoryDC創建圖形時失敗,但無法放入staticBitmap。

我花了大約一個星期的時間研究這個問題,閱讀了很多不同的教程和論壇,試圖找到同樣的問題。我覺得沒有其他人有這個問題。

當窗口大小調整時,必須重新繪製大部分工作的ClientDC,導致閃爍。以下是我對ClientDC:

self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY, 
            wx.EmptyBitmap(517,524)) 

def OnGoSelect(self,e): 
    print "GO" 
    img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG) 
    self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 

def DrawLine(self): 
    dc = wx.ClientDC(self.imageCtrl) 
    dc.SetPen(wx.Pen(wx.BLUE, 2)) 
    dc.DrawLines(((223, 376), (223, 39), (240, 39), (240,376), (223,376))) 

目前PaintDC不會進入一個無限循環,而是圖像被放置在staticBitmap並以某種方式繪製是背後的形象。因此,當我調整大小時,ComboBoxes會擦除部分圖像並調整窗口大小以覆蓋圖像,從而擦除該部分。當我縮回窗口時,圖形仍然存在,但圖像被擦除。下面是我有:

self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY, 
            wx.EmptyBitmap(517,524)) 

def OnGoSelect(self,e): 
    print "GO" 
    img = wx.Image("./poster/"+self.picChoice,wx.BITMAP_TYPE_PNG) 
    self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 

    self.imageCtrl.Bind(wx.EVT_PAINT, self.OnPaint) 

def OnPaint(self, e): 
    print "OnPaint Triggered" 
    dc = wx.PaintDC(self.imageCtrl) 
    dc.Clear() 
    dc.SetPen(wx.RED_PEN) 
    dc.DrawLines(((100, 200), (100, 100), (200, 100), (200,200), (100,200))) 

對於MemoryDC,我裝的EmptyBitmap全部由自己,借鑑了它,然後試圖把它放到staticBitmap。它給了我一個空白的灰色屏幕。如果我沒有使用EmptyBitmap,它會顯示正常,黑色。即使當我畫上它時,我仍然將它保存到一個文件中,但它仍然給了我應用程序中的灰色屏幕。這裏的MemoryDC代碼:

self.imageCtrl = wx.StaticBitmap(self.thePanel, wx.ID_ANY, 
            wx.EmptyBitmap(517,524)) 

def Draw(self, e): 
    print "Draw" 
    img = wx.Image("./poster/Test2.png", wx.BITMAP_TYPE_ANY) 
    bit = wx.EmptyBitmap(517,524) 
    dc = wx.MemoryDC(bit) 
    dc.SetBackground(wx.Brush(wx.BLACK)) 
    dc.Clear() 
    dc.SetPen(wx.Pen(wx.RED, 1)) 
    dc.DrawLines(((83, 375), (83, 42), (120, 42), (120,375), (83,375))) 
    self.imageCtrl.SetBitmap(bit) 
    bit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP) 

我在我的智慧結束。歡迎任何建議!

回答

4

我找到了!

在使用MemoryDC之前我並不知道,我必須取消選擇我正在繪製的位圖。這是通過將一個wx.NullBitmap傳遞給SelectObject方法完成的。

下面是MemoryDC代碼:

def Draw(self, e): 
    print "Draw" 
    img = wx.Image("./poster/Test2.png", wx.BITMAP_TYPE_ANY) 
    bit = wx.EmptyBitmap(517,524) 
    imgBit = wx.BitmapFromImage(img) 
    dc = wx.MemoryDC(imgBit) 
    dc.SetPen(wx.Pen(wx.RED, 1)) 
    dc.DrawLines(((83, 375), (83, 42), (120, 42), (120,375), (83,375))) 
    dc.SelectObject(wx.NullBitmap)# I didn't know I had to deselect the DC 
    self.imageCtrl.SetBitmap(imgBit) 
    imgBit.SaveFile("bit.bmp", wx.BITMAP_TYPE_BMP)