wxpython:从一个列表框插入多个字符串到另一个列表框
当我在第一个列表框(zone_list)中点击一个选项时,我想把这个选项添加到第二个列表框(time_zones2)里。如果我之后选择了另一个选项,我希望把这个新选项添加到第二个列表框的下一行。然后,当我在第二个列表框中点击之前添加的某个选项时,我想把这个选项删除。
我想实现的功能是: 点击第一个列表框中的选项 -> 把这个选项添加到第二个列表框 点击第二个列表框中的选项 -> 从第二个列表框中删除这个选项
看看我下面写的代码:
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()
1 个回答
0
我把你的代码修改了一下,加入了你需要的部分。要记住,wx.ListBox.Set(items) 这个函数是需要一个列表的,也就是说你传给它一个字符串的时候,它会把这个字符串里的每个字符都当成一个单独的项目来处理。
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()