快速填充wx.Choice的方法?

1 投票
1 回答
2621 浏览
提问于 2025-04-18 12:53

我需要在一个 wx.Choice 里添加几百个选项,看起来只有一个方法可以做到,那就是 Append()

choiceBox = wx.Choice(choices=[], id = wx.ID_ANY, parent=self, pos=wx.Point(0, 289),
                                          size=wx.Size(190, 21), style=0)

for item in aList:
    choiceBox.Append(item)

我试过一次性添加整个列表,但这样不行,有没有更好的方法呢?

1 个回答

4

什么???你只是给它选择而已

choiceBox = wx.Choice(choices=aList, id = wx.ID_ANY, parent=self, pos=wx.Point(0, 289),
                                      size=wx.Size(190, 21), style=0)

你也可以稍后再做这件事

choicebox.SetItems(aList)

这里有个简单的例子,生成选择的过程很耗时,但我们使用了线程,这样就不会让界面卡住

import wx
import threading 
import time
import random
def make_choices():
    choices = []
    for _ in range(80):
        choices.append(str(random.randint(0,1000000)))
        time.sleep(0.1)
        print "Making choice List!"
    return choices

def make_choice_thread(wxChoice,choice_fn):
    wx.CallAfter(wxChoice.SetItems,choice_fn())
    wx.CallAfter(wxChoice.SetSelection,0)

a = wx.App(redirect=False)
fr = wx.Frame(None,-1,"A Big Choice...")
st = wx.StaticText(fr,-1,"For Some reason you must pick from a large list")
ch = wx.Choice(fr,-1,choices=["Loading...please wait!"],size=(200,-1),pos=(15,15))
ch.SetSelection(0)
t = threading.Thread(target=make_choice_thread,args=(ch,make_choices))
t.start()
fr.Show()
a.MainLoop()

撰写回答