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

发布于 2024-12-29 08:44:55 字数 1215 浏览 0 评论 0原文

当我单击 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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他夏了夏天 2025-01-05 08:44:55

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文