【问题标题】:Python gtk: ComboBoxEntry with EnumPython gtk:带枚举的 ComboBoxEntry
【发布时间】:2014-06-25 08:54:26
【问题描述】:

我真的在努力寻找一个示例来创建 Enum 并将其作为模型发送到 ComboBoxEntry。

有人可以上传正确方法的示例吗?

这是我的代码的开头:

model = "one", "two", "three"
liststore = gtk.ListStore(str)
for item in model:
    liststore.append([item])
cbe = gtk.ComboBoxEntry(liststore)

我希望 model 成为能够编写的枚举:

# for example:
cbe.set_active(one)
# or
if cbe.get_active() == model.one: ...

非常感谢

【问题讨论】:

    标签: python-2.7 combobox enums pygtk


    【解决方案1】:

    我用这个:

        ##  Length
        nLines      = tableData.data.shape[0]
        ##  Width
        nColons     = len(tableData.columns)
        ##  +1 adds an extra column for numbering (at the left side)
        types       = [str] * (nColonnes + 1)
        ##  build it
        modelT      = gtk.ListStore( * types )
    

    这应该适合你:

    >>> model = ["one", "two", "three"]
    >>> types = [str] * len(model)
    >>> lists = gtk.ListStore( * types )
    

    【讨论】:

    • 对不起,我只想让“模型”成为枚举。所以当我写: myComboBoxEntry.set_active(one) 它会工作
    【解决方案2】:

    Python 3.4 有一个新的Enum 数据类型(也可以使用enum34 backportadvanced enum library)。安装enum34aenum

    • pip install enum34,或
    • pip install aenum

    之后,您可以将model 设为Enum,如下所示:

    from enum import Enum    # or from aenum import Enum
    
    class Model(str, Enum):
        one = 'one'
        two = 'two'
        three = 'three'
    

    这些结果:

    >>> list(Model)
    [<Model.one: 'one'>, <Model.two: 'two'>, <Model.three: 'three'>]
    
    >>> Model.one
    <Model.one: 'one'>
    
    >>> Model.one == 'one'
    True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2012-09-22
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2020-04-19
      相关资源
      最近更新 更多