繁体   English   中英

是否可以将列表“设置”到ComboBox,wxpython?

[英]Is it is possible to “set” a list to a ComboBox, wxpython?

嗨,我知道有可能用列表做到这一点,但是有可能用组合框做到这一点吗? 有什么和set函数类似的东西吗?

我尝试将set与组合框一起使用,但收到以下错误:AttributeError:'ComboBox'对象没有属性'Set'

谢谢。

好了,您可以调用SetItems(myList)将列表放入ComboBox,覆盖其中的内容。

编辑:在组合框的列表中创建列表的最常见方法是这样的:

myList = ["dog", "cat", "hamster"]
cbo = wx.ComboBox(self, choices=myList)

但是由于ComboBox继承自ItemContainer,因此您也可以像下面的完整示例一样进行操作:

import wx

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")
        panel = wx.Panel(self)

        myList = ["dog", "cat", "hamster"]
        cbo = wx.ComboBox(panel)
        cbo.SetItems(myList)

        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

http://www.wxpython.org/docs/api/wx.ComboBox-class.html

__init__(parent, id, value, pos, size, choices, style, validator, name)

combobox = wx.ComboBox(self, choices=myList)

我相信您正在要求一种“在运行时”添加新项目的方法? 即表格创建后? 如果是这样,请参见下面的代码;-)

   def UpdateCitiesCombo(self):
    self.cmbCities.Clear()
    pc = PostalCode()
    if self.txtPostalCode.Value:
        cities = pc.GetFromCode(int(self.txtPostalCode.Value))
        for city in cities:
            self.cmbCities.Append(city[2])

    items = self.cmbCities.GetItems()

    index = -1
    try:
        if self.customer.city != "":
            index = items.index(self.customer.city)
        else:
            index = 0

        self.cmbCities.SetSelection(index)

    except ValueError:
        self.cmbCities.SetValue(self.customer.city)

本质上,您不应该使用ComboBox的Clear()和Append()方法,以及从某个地方的事件调用此函数的事实。 希望它是您想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM