Skip to main content
 首页 » 编程设计

wxPython 一次选择多个项目 Listctrl

2024年08月06日10Leo_wl

我正在尝试创建一个自定义 wxPython 小部件。这将允许用户从左侧列表中选择多个项目并将它们移动到右侧列表。 我只是坚持从列表中选择多个项目。 这是我想要实现的屏幕截图:

这是我的代码(它刚刚开始,因为没有清理):

import wx 
 
######################################################################## 
class Car: 
    """""" 
 
    #---------------------------------------------------------------------- 
    def __init__(self, id, model, make, year): 
        """Constructor""" 
        self.id = id 
        self.model = model 
        self.make = make 
        self.year = year        
 
 
######################################################################## 
class MyForm(wx.Frame): 
 
    #---------------------------------------------------------------------- 
    def __init__(self): 
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(800,600)) 
 
        # Add a panel so it looks the correct on all platforms 
        panel = wx.Panel(self, wx.ID_ANY) 
 
        ford = Car(0, "Ford", "F-150", "2008") 
        chevy = Car(1, "Chevrolet", "Camaro", "2010") 
        nissan = Car(2, "Nissan", "370Z", "2005") 
        fiat = Car(2, "Fiat", "F7Z", "2005") 
        fiat2 = Car(2, "Fiat", "punto", "2005") 
 
        sampleList = [] 
 
        lb = wx.ListBox(panel, 
                        size=(200, 150), 
                        choices=sampleList) 
        self.oneadd = wx.Button(panel,-1, ">", pos=(110, 180)) 
        self.multiadd = wx.Button(panel, -1,">>",pos=(200, 180)) 
 
        lb2 = wx.ListBox(panel, 
                size=(200, 150), 
                choices=sampleList) 
 
        self.lb = lb 
        self.lb2 = lb2 
        lb2.Append(ford.make, ford) 
        lb.Append(chevy.make, chevy) 
        lb.Append(fiat.make, fiat) 
        lb.Append(fiat2.make, fiat2) 
        lb.Append(nissan.make, nissan) 
        lb.Bind(wx.EVT_LISTBOX, self.onSelect) 
 
 
        sizer = wx.BoxSizer(wx.HORIZONTAL) 
 
        sizer.Add(lb, 0, wx.ALL, 5) 
 
        sizer.Add(lb2, 0, wx.ALL, 5) 
        panel.SetSizer(sizer) 
 
    #---------------------------------------------------------------------- 
    def onSelect(self, event): 
        """""" 
        print "You selected: " + self.lb.GetStringSelection() 
        obj = self.lb.GetClientData(self.lb.GetSelection()) 
        text = """ 
        The object's attributes are: 
        %s  %s    %s  %s 
 
        """ % (obj.id, obj.make, obj.model, obj.year) 
        print text 
 
# Run the program 
if __name__ == "__main__": 
    app = wx.App(False) 
    frame = MyForm() 
    frame.Show() 
    app.MainLoop() 

请您参考如下方法:

您需要在列表框中启用多重选择

lb = wx.ListBox(panel, 
                size=(200, 150), 
                style=wx.LB_MULTIPLE, 
                choices=sampleList) 

洛克拉