如何使用OnClick事件填充wxpython LIstCtrl

时间:2017-06-22 05:59:58

标签: python-2.7 events onclick wxpython listctrl

我想设计一个wxpython ListCtrl。因此,当点击搜索按钮时,我会得到这样的列表

    [(7, u'GLUOCOSE', u'C6H1206'), (8, u'SUCROSE', u'C12H22O11')]

我想用上面的输出填充listctrl。我是InsertStringItem方法,它将返回当前行的索引,其余列由SetStringItem()方法填充。但是这给了我TypeError:String或Unicode类型required.How我能做到这一点吗?

def OnSearch(self, event):        
    placeholder = '?'
    placeholders = ','.join(placeholder for unused in self.molecule_list)
    query = 'SELECT * FROM MOLECULE WHERE MOL_NUMBER IN (%s)' % placeholders
    cursor = self.conn.execute(query, self.molecule_list)
    final = cursor.fetchall()
    print final 
    for j in final: 
        index = self.list.InsertStringItem(sys.maxint, j[0]) 
        self.list.SetStringItem(index, 1, j[1]) 
        self.list.SetStringItem(index, 2, j[2]) 

1 个答案:

答案 0 :(得分:1)

您正在迭代游标,而不是变量public class Program { static void Main(string[] args) { Instance inst= new Instance(); inst.AddValue(); Service serv= new Service(Instance.staticDictnry); inst.AddValue2(); serv.DoSomething(); Console.ReadLine(); } } public class Instance { public static ConcurrentDictionary<string, String> staticDictnry= new ConcurrentDictionary<string, string>(); private Service s1; public Instance() { s1 = new Service(staticDictnry); } public void AddValue() { staticDictnry.TryAdd("A", "A1"); } public void AddValue2() { staticDictnry.TryAdd("B", "B1"); } } public class Service { private ConcurrentDictionary<string, string> dictionary; public Service(ConcurrentDictionary<string, string> _dictionary) { dictionary = _dictionary; } public void DoSomething() { foreach (var keyValuePair in dictionary) { Console.WriteLine("Value " + keyValuePair.Key + " Key "+ keyValuePair.Value); } } } 中返回的数据。切换使用final

进行迭代
final

最简单的方法是确保index = 0 for j in final: index = self.list.InsertStringItem(index, str(j[0])) self.list.SetStringItem(index, 1, j[1]) self.list.SetStringItem(index, 2, j[2]) index +=1 ListCtrl,然后使用style=wx.LC_REPORT

Append

其中j [n]是for j in final: self.list.Append((j[0],j[1],j[2],j[3])) 中项目数量所需的每个项目。我希望这是有道理的。

以下是显示两种方法的示例

ListCtrl