2010-04-06 40 views
3

我想繪製一個以wx.EmptyBitmap爲中心的數字。如何使用wxpython在位圖中繪製文本?

我該如何使用wxpython來做到這一點?

感謝提前:)

import wx 

app = None 

class Size(wx.Frame): 
    def __init__(self, parent, id, title): 
     frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200)) 
     bmp = wx.EmptyBitmap(100, 100) 
     dc = wx.MemoryDC() 
     dc.SelectObject(bmp) 
     dc.DrawText("whatever", 50, 50) 
     dc.SelectObject(wx.NullBitmap) 
     wx.StaticBitmap(self, -1, bmp) 
     self.Show(True) 


app = wx.App() 
Size(None, -1, 'Size') 
app.MainLoop() 

此代碼只給了我黑色的形象,我究竟做錯了什麼? 現在缺少在這裏..

+0

你需要清除位圖到合適的顏色,並繪製文本在適當的顏色... – Will 2010-04-06 12:50:25

+0

確切的人:)它的工作;)謝謝你們! – 2010-04-06 12:52:45

回答

5

選擇在wx.MemoryDC的BMP,對DC畫什麼,然後選擇位圖進行例如

import wx 

app = None 

class Size(wx.Frame): 
    def __init__(self, parent, id, title): 
     frame = wx.Frame.__init__(self, parent, id, title, size=(250, 200)) 
     w, h = 100, 100 
     bmp = wx.EmptyBitmap(w, h) 
     dc = wx.MemoryDC() 
     dc.SelectObject(bmp) 
     dc.Clear() 
     text = "whatever" 
     tw, th = dc.GetTextExtent(text) 
     dc.DrawText(text, (w-tw)/2, (h-th)/2) 
     dc.SelectObject(wx.NullBitmap) 
     wx.StaticBitmap(self, -1, bmp) 
     self.Show(True) 


app = wx.App() 
app.MainLoop() 
+0

Anurag,你能告訴我什麼是錯誤的代碼,我發佈PLZ :) – 2010-04-06 12:48:26

+0

@aF,我有更新的代碼,所以現在清除了直流,我計算正確的垂直和水平居中的文本位置 – 2010-04-06 13:18:28

1

您使用wx.MemoryDC

+0

你可以給我一些代碼示例:) – 2010-04-06 09:26:49

+1

有wxPython演示中的例子的負載。只是一個擡頭 – 2010-04-06 16:41:19