wxpython:插入多个从一个列表框到另一个列表框的字符串

2024-10-01 07:44:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在timezone1列表框中单击zone_列表的其中一个选项时,我想在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()

Tags: importgtselfzonetimedef选项frame
1条回答
网友
1楼 · 发布于 2024-10-01 07:44:16

我拿走了你的代码,加上了你需要的。请记住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()

相关问题 更多 >