是否可以“设置”组合框的列表,wxpython?

发布于 2024-12-16 21:53:53 字数 143 浏览 1 评论 0原文

您好,我知道可以使用列表来执行此操作,但是可以使用组合框执行此操作吗?有没有类似set的函数?

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

谢谢。

Hi i know its possible to do this with lists however is it possible to do this with Comboboxes? Is there anything similar to the set function?

I have tried using set with a Combo box but i receive the following error:
AttributeError: 'ComboBox' object has no attribute 'Set'

Thanks.

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

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

发布评论

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

评论(3

你是暖光i 2024-12-23 21:53:53

那么,您可以调用 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()

Well, you can call SetItems(myList) to put a list into a ComboBox, overwriting what's already in it.

EDIT: The most common method to create a list in a combobox's list is like this:

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

But since ComboBox inherits from ItemContainer, you can also do it like this complete example:

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()
绅刃 2024-12-23 21:53:53

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

__init__(父、id、值、位置、大小、选择、样式、验证器、名称)

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

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)
ㄟ。诗瑗 2024-12-23 21:53:53

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

   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() 方法,以及从某处的事件调用该函数的事实。希望这是您正在寻找的。

I believe your are asking for a method to add new items "at runtime"? ie after the form is created? See the code below if so ;-)

   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)

In essence what you should not is the Clear() and Append() methods of the ComboBox and the fact that this function is called from an event somewhere. Hope it is what you are looking for.

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