【问题标题】:what is meaning of string inside array python数组python中的字符串是什么意思
【发布时间】:2017-12-03 00:30:45
【问题描述】:

倒数第二行的“CmdBtn['menu'] = CmdBtn.menu”是什么意思。

def makeCommandMenu():
    CmdBtn = Menubutton(mBar, text='Button Commands', underline=0)
    CmdBtn.pack(side=LEFT, padx="2m")
    CmdBtn.menu = Menu(CmdBtn)
    ...
    ...
    CmdBtn['menu'] = CmdBtn.menu
    return CmdBtn

【问题讨论】:

  • 表示CmdButton.__setitem__('menu', CmdBtn.menu),显然是为给定键设置资源值。
  • 欢迎来到stackoverflow。您的问题可能因为两个原因而被大量否决:1. 它没有为您的问题提供上下文,以及 2. 它询问的是 python 的一个非常基本的部分(括号 [] 运算符的含义),这意味着您要么懒得看教程,要么你问'menu' 键对于MenuButton 对象的具体含义是什么。如果是最后一种情况,您需要在问题中更加明确(参见第 1 点)。如果它是第一个,这将有助于更明确地说明您要问的究竟是什么。

标签: python tkinter


【解决方案1】:

CmdBtn.configure(menu=CmdBtn.menu)的简写

设置小部件选项的方法通常是在创建时(例如:Menubutton(..., menu=...))或使用configure 方法(例如:CmdBtn.configure(menu=...)。Tkinter 提供了第三种方法,即将小部件视为字典其中配置值是字典的键(例如:CMdBtn['menu']=...

这在官方 python tkinter 文档的Setting Options 部分中有介绍

【讨论】:

    【解决方案2】:

    首先,在 Python 中,everything is an object 和方括号表示该对象可下标(例如tuplelistdictstring 等等) .可订阅意味着这个对象至少实现了__getitem__() 方法(在你的情况下是__setitem__())。

    使用这些方法可以很容易地与类成员进行交互,所以不要害怕构建自己的示例,以了解其他人的代码。

    试试这个 sn-p:

    class FooBar:
        def __init__(self):
            #   just two simple members
            self.foo = 'foo'
            self.bar = 'bar'
    
        def __getitem__(self, item):
            #   example getitem function
            return self.__dict__[item]
    
        def __setitem__(self, key, value):
            #   example setitem function
            self.__dict__[key] = value
    
    #   create an instance of FooBar
    fb = FooBar()
    
    #   lets print members of instance
    #   also try to comment out get and set functions to see the difference
    print(fb['foo'], fb['bar'])
    
    #   lets try to change member via __setitem__
    fb['foo'] = 'baz'
    
    #   lets print members of instance again to see the difference
    print(fb['foo'], fb['bar'])
    

    【讨论】:

      【解决方案3】:

      这不是“数组中的字符串”。

      方括号运算符用于以某种序列(通常是list,或tuple)、映射(通常是dict,或字典)或某种其他类型的特殊对象(例如这个MenuButton 对象,它不是序列或映射)。与其他一些语言不同,在 python 中,任何对象都可以使用这个操作符。

      list 类似于其他语言中的“数组”。它可以包含任何类型的对象的混合物,并保持对象的顺序。当您想要维护有序的对象序列时,list 对象非常有用。您可以使用其索引访问list 中的对象,如下所示(索引从零开始):

      x = [1,2,3] # this is a list
      assert x[0] == 1 # access the first item in the list
      x = list(range(1,4)) # another way to make the same list
      

      dict(字典)在您想将值与键关联时很有用,以便以后可以使用键查找值。像这样:

      d = dict(a=1, b=2, c=3) # this is a dict
      assert x['a'] == 1 # access the dict
      d = {'a':1, 'b':2, 'c':3} # another way to make the same dict
      

      最后,您可能还会遇到同样使用相同项目访问接口的定制对象。在Menubutton 的情况下,['menu'] 只是访问响应密钥'menu' 的某个项目(由 tkinter API 定义)。您也可以使用 item-access 创建自己的对象类型(下面的 python 3 代码):

      class MyObject:
          def __getitem__(self, x):
              return "Here I am!"
      

      这个对象除了为你给它的键或索引值返回相同的字符串之外没有做太多的事情:

      obj = MyObject()
      print(obj [100]) # Here I am!
      print(obj [101]) # Here I am!
      print(obj ['Anything']) # Here I am!
      print(obj ['foo bar baz']) # Here I am!
      

      【讨论】:

        【解决方案4】:

        当您使用x[y] = z 时,它会调用__setitem__ 方法。

        x.__setitem__(y, z)
        

        在您的情况下,CmdBtn['menu'] = CmdBtn.menu 表示

        CmdBtn.__setitem__('menu', CmdBtn.menu)
        

        Menubutton 类确实提供了__setitem__ method。看起来这是用于为给定键('menu')设置“资源值”(在本例中为 CmdBtn.menu)。

        【讨论】:

          猜你喜欢
          • 2010-09-29
          • 1970-01-01
          • 2022-04-08
          • 1970-01-01
          • 2018-08-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-02
          相关资源
          最近更新 更多