2012-07-20 32 views
0

我想打一個網格是這樣的:如何在wxPython中創建一個網格?

enter image description here

不是wx.Grid。有誰知道如何做到這一點,並可以提供一個例子?

這將用於GridSizer或GridBagSizer分離小部件。

+0

你只是想抽出一個網格,或者創建一個小部件網格? – 2012-07-20 17:21:05

回答

1

使用幀的繪圖上下文 wxWindow子類(框架/面板/等等......實際上幾乎任何wxObject)。

做的

一種方式是這樣的

import wx 
class MyCustomFrame(wx.Frame): 
    def __init__(self,parent,id): 
     wx.Frame.__init__(self,parent,id) 
     self.Bind(wx.EVT_PAINT,self.OnPaint) 
    def OnPaint(self,evt): 
     self.dc = dc = wx.PaintDC(self) 
     p1 = [0,0] 
     p2 = [500,0] 
     for i in range(100): 
      dc.DrawLine(p1[0],p1[1],p2[0],p2[1]) 
      p1 = [p1[0],p1[1]+5] 
      p2 = [p2[0],p2[1]+5] 
     p1=[0,0] 
     p2 = [0,500] 
     for i in range(100): 
      dc.DrawLine(p1[0],p1[1],p2[0],p2[1]) 
      p1 = [p1[0]+5,p1[1]] 
      p2 = [p2[0]+5,p2[1]] 
if __name__ == "__main__": 
    a = wx.App(redirect=False) 
    f = MyCustomFrame(None,-1) 
    #f.OnPaint(None) --- dont do this!!! 
    f.Show() 
    a.MainLDop() 

您可以通過使用drawlines加快步伐,而不是

def OnPaint(self,evt): 
     self.dc = dc = wx.PaintDC(self) 
     verticle_lines = [(i*5,0,i*5,500) for i in range(100)] 
     horizontal_lines = [(0,i*5,500,i*5) for i in range(100)] 
     dc.DrawLineList(horizontal_lines+verticle_lines) 

爲了做你想做的與電網分級機,你可以做到這一點

import wx 
class MyCustomPanel(wx.Panel): 
    def __init__(self,parent,id): 
     wx.Panel.__init__(self,parent,id) 
     self.sz = wx.GridSizer(5,5,0,0) 
     for i in range(25): 
      self.sz.Add(wx.StaticText(self,-1,str(i))) 
     self.SetSizer(self.sz) 
     self.Bind(wx.EVT_PAINT,self.OnPaint) 
    def OnPaint(self,evt): 
     self.dc = dc = wx.PaintDC(self) 
     w,h = self.sz.GetSize() 
     nr = self.sz.GetRows() 
     nc = self.sz.GetCols() 
     cell_w = float(w)/nc 
     cell_h = float(h)/nr 
     hlines = [(0,i*cell_h,w,i*cell_h)for i in range(nr+1)] 
     vlines = [(i*cell_w,0,i*cell_w,h)for i in range(nc+1)] 
     self.dc.DrawLineList(hlines+vlines) 

if __name__ == "__main__": 
    a = wx.App(redirect=False) 
    f1 = wx.Frame(None,-1) 
    f = MyCustomPanel(f1,-1) 
    #f.OnPaint(None) 
    f1.Show() 
    a.MainLoop() 

你也可以用這樣的風格完成這個

import wx 
class SimplePanel(wx.Panel): 
    def __init__(self,parent,id,str_val): 
     wx.Panel.__init__(self,parent,id,style=wx.SIMPLE_BORDER) 
     self.sz = wx.BoxSizer() 
     self.sz.Add(wx.StaticText(self,-1,str_val),0,wx.ALIGN_CENTER) 
     self.SetSizer(self.sz) 
class MyCustomPanel(wx.Panel): 
    def __init__(self,parent,id): 

     wx.Panel.__init__(self,parent,id) 
     self.sz = wx.GridSizer(5,5,0,0) 
     for i in range(25): 
      self.sz.Add(SimplePanel(self,-1,str(i)),0,wx.GROW) 
     self.SetSizer(self.sz) 

if __name__ == "__main__": 
    a = wx.App(redirect=False) 
    f1 = wx.Frame(None,-1) 
    f = MyCustomPanel(f1,-1) 
    #f.OnPaint(None) 
    f1.Show() 
    a.MainLoop() 
+0

噢,很好,你知道如何使它與GridSizer或GridBagSizer一起工作來分離小部件嗎? (如按鈕) – user1513192 2012-07-20 02:43:55

+0

或我怎樣才能覆蓋GridSizer? – user1513192 2012-07-20 02:50:35

+0

使用一個面板而不是一個框架,並給它使用SetMinSize((w,h))給它的尺寸一個sizer會識別..哦,我看到你的問題...這將需要一段時間來回答... – 2012-07-20 02:55:29