Search code examples
pythonwxpythonwxwidgets

wxpython:Insert multiple strings from one listbox to an other listbox


When I click on one of the choices of the zone_list in timezone1 listbox, I want to insert that string in the time_zones2 listbox, and if I select an other choice after that, I want to add the second choice to the second line of the timezones2 listbox. Then, when I click one of the choices I have done before to the time_zone2 listbox, I want to delete that choice.

This is what I want to do: listbox1 click on a choice->insert that choice in listbox2 listbox2 click on a choice->delete that choice from listbox2

Look what I have done below:

import wx

from time import *

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, (550, 350))

        zone_list = ['CET', 'GMT', 'MSK', 'EST', 'PST', 'EDT']


        panel = wx.Panel(self, -1)
        self.time_zones = wx.ListBox(panel, -1, (10,100), (170, 130), zone_list, wx.LB_SINGLE)
        self.time_zones.SetSelection(0)

        self.time_zones2 = wx.ListBox(panel, -1, (10,200), (170, 400), '',wx.LB_SINGLE)

        self.Bind(wx.EVT_LISTBOX, self.OnSelect)

    def OnSelect(self, event):

        index = event.GetSelection()
        time_zone = self.time_zones.GetString(index)


        self.time_zones2.Set(time_zone)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, 'listbox.py')
        frame.Centre()
        frame.Show(True)
        return True

app = MyApp(0)
app.MainLoop()

Solution

  • I took your code and added what you needed. Keep in mind that wx.ListBox.Set(items) expects a list of items, so when you pass it a single string, it will consider each character from the string as a separate item.

    import wx
    
    from time import *
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, (550, 350))
            self.second_zones = []
            zone_list = ['CET', 'GMT', 'MSK', 'EST', 'PST', 'EDT']
    
    
            panel = wx.Panel(self, -1)
            self.time_zones = wx.ListBox(panel, -1, (10,100), (170, 130), zone_list, wx.LB_SINGLE)
            self.time_zones.SetSelection(0)
    
            self.time_zones2 = wx.ListBox(panel, -1, (10,200), (170, 400), '',wx.LB_SINGLE)
    
            self.Bind(wx.EVT_LISTBOX, self.OnSelectFirst, self.time_zones)
            self.Bind(wx.EVT_LISTBOX, self.OnSelectSecond, self.time_zones2)
    
    
        def OnSelectFirst(self, event):
            index = event.GetSelection()
            time_zone = str(self.time_zones.GetString(index))
            self.second_zones.append(time_zone)
            self.time_zones2.Set(self.second_zones)
    
    
        def OnSelectSecond(self, event):
            index = event.GetSelection()
            time_zone = str(self.time_zones2.GetString(index))
            self.second_zones.remove(time_zone)
            self.time_zones2.Set(self.second_zones)        
    
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, 'listbox.py')
            frame.Centre()
            frame.Show(True)
            return True
    
    app = MyApp(0)
    app.MainLoop()