【问题标题】:wxpython:Insert multiple strings from one listbox to an other listboxwxpython:将多个字符串从一个列表框插入到另一个列表框
【发布时间】:2012-01-26 23:55:14
【问题描述】:

当我单击 timezone1 列表框中 zone_list 的选项之一时,我想将该字符串插入 time_zones2 列表框中,如果之后选择其他选项,我想将第二个选项添加到第二行timezones2 列表框。然后,当我单击之前对 time_zone2 列表框所做的选择之一时,我想删除该选择。

这就是我想要做的: listbox1 单击一个选项-> 在 listbox2 中插入该选项 listbox2 单击一个选项->从 listbox2 中删除该选项

看看我在下面做了什么:

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()

【问题讨论】:

    标签: python wxpython wxwidgets


    【解决方案1】:

    我获取了您的代码并添加了您需要的内容。请记住,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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多