2016-04-10 39 views
0

我有一個簡單的應用程序,顯示wx.VListBox中的用戶信息列表 每個項目都使用OnDrawItem()呈現,並且當我需要繪製文字,線條或圖像。在wx.VListBox中使用控件繪製自定義面板OnDrawItem()

該應用程序的外觀:

enter image description here

如何幾個文本標籤(姓名和電子郵件)也很少旁邊wx.ComboBox,很少wx.ComboBox添加到每一行?

我將解決方案看作是基於wx.Panel呈現一些自定義控件,該控件具有在該面板上佈置的所有控件,但如何使用它的子控件在wx.VListBox的每一行上呈現面板?

我當前的完整代碼(只是複製/粘貼):

import wx 


class UserInfo(object): 
    def __init__(self, name, email, *args, **kwargs): 
     super(self.__class__, self).__init__(*args, **kwargs) 

     self.name = name 
     self.email = email 


class UserListBox(wx.VListBox): 
    def __init__(self, parent, users, *args, **kwargs): 
     super(self.__class__, self).__init__(parent, *args, **kwargs) 

     self.bh = 10 
     self.users = users 

     self.SetItemCount(len(self.users)) 

    def OnMeasureItem(self, idx): 
     image_height = self.bh + 4 
     name_size = self.GetTextExtent(self.users[idx].name) 
     email_size = self.GetTextExtent(self.users[idx].email) 
     return max(image_height, name_size[1] + email_size[1] + 6) 

    def OnDrawSeparator(self, dc, rect, idx): 
     oldpen = dc.GetPen() 
     dc.SetPen(wx.Pen(wx.BLACK)) 
     dc.DrawLine(rect.x, rect.y, rect.x + rect.width, rect.y) 
     rect.Deflate(0, 2) 
     dc.SetPen(oldpen) 

    def OnDrawItem(self, dc, rect, n): 
     # Draw the name label to the right of the bitmap 
     textx = rect.x + 2 + self.bh + 2 
     lblrect = wx.Rect(textx, rect.y, 
          rect.width - textx, 
          rect.height) 
     user = self.users[n] 
     dc.DrawLabel(user.name, lblrect, 
        wx.ALIGN_LEFT | wx.ALIGN_TOP) 
     dc.DrawLabel(user.email, lblrect, 
        wx.ALIGN_LEFT | wx.ALIGN_BOTTOM) 


class AppFrame(wx.Frame): 
    def __init__(self, parent, *args, **kwargs): 
     super(self.__class__, self).__init__(parent, *args, **kwargs) 

     sizer = wx.BoxSizer(wx.VERTICAL) 
     self.SetSizer(sizer) 

     users = list() 
     for n in range(100): 
      users.append(UserInfo('John %s' % n, '[email protected]')) 

     listobx = UserListBox(self, users, size=(150, 250), style=wx.BORDER_SUNKEN) 
     sizer.Add(listobx, flag=wx.EXPAND) 

     btn = wx.Button(self, label='Do it!') 
     sizer.Add(btn) 


class DemoApp(wx.App): 
    def OnInit(self): 
     self.frame = AppFrame(None, title="App Demo") 
     self.frame.Show() 
     return True 

if __name__ == '__main__': 
    app = DemoApp() 
    app.MainLoop() 

回答

1

可以使用wxRendererNative類的方法繪製的自定義控件,例如原生的前瞻性陳述在你的情況下,你會使用DrawComboBoxDropButton()。無論何時單擊圖像(或者甚至當鼠標懸停在圖像上時),您都必須創建一個真實的wxComboBox以允許用戶更改其值。

+0

謝謝,有道理。但如何跟蹤點擊/懸停整個列表框單元格上的特定圖像?而且更有趣的是 - 如何在相同座標上實現真實控制的圖像並再次移除控制。你能用代碼示例建議任何地方嗎? – Zelid

+0

您可以綁定到鼠標移動事件並使用'VirtualHitTest()'獲取它下面的項目,但是如果您可以完全用真正的組合框重疊單元格,那麼找到組合按鈕的確切位置可能會非常棘手,將是最簡單的解決方案。取消控制很簡單:只需在失去焦點時進行。 –

相關問題