2011-07-01 74 views
0

我需要一個ListBox來顯示電話簿。然後我需要在每個列表項中顯示名稱頂部和底部數字,如phone.how將數據綁定到列表框。如何製作多行wx.ListBox

現在我做了單行列表框如下圖所示

cur.execute("select fname from tblsample1 order by fname") 
    names = [str(item[0]) for item in cur.fetchall()] 
    lvnames=wx.ListBox(panel,-1,(10,40),(210,180),names, wx.LB_SINGLE) 

如何sqlite3的光標兩列綁定到ListView

我需要一個wx.ListBox鼠標點擊事件(不EVT_LISTBOX 因爲我只需要鼠標點擊事件)

回答

3

使用HtmlListBox,這裏是一個小例子讓你開始。

import wx 


class PhoneNumbers(wx.HtmlListBox): 
    def __init__(self, parent): 
     wx.HtmlListBox.__init__(self, parent) 
     self.data = [ 
      ("Foo", "3452-453"), 
      ("Bar", "5672-346"), 
     ] 
     self.SetItemCount(len(self.data)) 

    def OnGetItem(self, n): 
     return "<b>%s</b><br>%s" % self.data[n] 

    def add_number(self, name, number): 
     self.data.append((name, number)) 
     self.SetItemCount(len(self.data)) 
     self.Refresh() 


class Frame(wx.Frame): 
    def __init__(self): 
     wx.Frame.__init__(self, None, size=(200, 400)) 

     self.numbers = PhoneNumbers(self) 
     self.contact_name = wx.TextCtrl(self) 
     self.contact_number = wx.TextCtrl(self) 
     self.add_btn = wx.Button(self, label="Add contact") 

     self.Sizer = wx.BoxSizer(wx.VERTICAL) 
     self.Sizer.Add(self.numbers, 1, wx.EXPAND) 
     self.Sizer.Add(wx.SearchCtrl(self), 0, wx.EXPAND) 
     self.Sizer.Add(wx.StaticText(self, label="Name"), 0, wx.TOP, 10) 
     self.Sizer.Add(self.contact_name) 
     self.Sizer.Add(wx.StaticText(self, label="Number"), 0, wx.TOP, 5) 
     self.Sizer.Add(self.contact_number) 
     self.Sizer.Add(self.add_btn, 0, wx.ALL, 10) 

     self.numbers.Bind(wx.EVT_LISTBOX, self.OnSelectNumber) 
     self.add_btn.Bind(wx.EVT_BUTTON, self.OnAddNumber) 

    def OnSelectNumber(self, event): 
     name, number = self.numbers.data[event.Selection] 
     self.contact_name.Value = name 
     self.contact_number.Value = number 

    def OnAddNumber(self, event): 
     self.numbers.add_number(
      self.contact_name.Value, 
      self.contact_number.Value 
     ) 


app = wx.PySimpleApp() 
app.TopWindow = f = Frame() 
f.Show() 
app.MainLoop() 
+0

如何將兩個TextCtrl添加到HtmlListBox? –

+0

您不能使用ListBox進行編輯,請查看wxGrid。 –

+0

如何將兩個wx.StaticText添加到HtmlListBox ???'Thank You' –

1

你應該改變你的問題,我不知道我是否得到這個權利。

如果你只需要在你的ListBox顯示兩行,你可以簡單地用一個\n

cur.execute("select fname,number from tblsample1 order by fname") 
entries = [str(item[0])+'\n'+str(item[1]) for item in cur.fetchall()] 

爲了得到一個「click」事件,你不能你wx.ListBox的樣式設置爲wx.LC_SINGLE_SEL和趕上選擇事件wx.EVT_LIST_ITEM_SELECTED

+0

我需要的FNAME應以粗體字母顯示,它應該比number.'Thank You' –

+0

更大然後,你應該用'HtmlListBox'去,因爲託尼Ruza提及。 Hava看演示,看起來很有希望。 – any1

+0

請給HtmlListBox演示 – 2011-07-04 08:07:43