Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a translation of what you wanted to do:</p> <pre><code>for labeldic in [{"label":"Cut"},{"label":"Copy"},{"label":"Paste"},{"label":"Delete"}]: labeldic.update({"state": "disabled"}) editmenu.add_command(**labeldic) </code></pre> <p>There were three problems I fixed. </p> <p>The first is that dictionary keys need to be quoted if they are strings. If you want a dict mapping the string <code>'label'</code> to the the string <code>'cut'</code>, you can do it using the dict literal <code>{'label': 'cut'}</code>, or else possibly with the <code>dict()</code> constructor which expands keyword arguments that way: <code>dict(label='cut')</code>. As you discovered, <code>{label: 'cut'}</code> wouldn't work, because it tries to use a variable's value for the key, but there is no such variable.</p> <p>The second is that you can't merge dictionaries using the + operator. It doesn't work, unfortunately. There is, however, an <a href="http://docs.python.org/library/stdtypes.html#dict.update" rel="nofollow">update method</a> that mutates the dict it's called on. Since it doesn't return a merged dict, it can't be used inline the way you used +.</p> <p>The third problem is that passing a dict is not the same as passing in keyword arguments. <code>foo(bar='baz')</code> is not the same as <code>foo({'bar':'baz'})</code>, but it <em>is</em> the same as <code>foo(**{'bar':'baz'})</code>. The <code>**</code> syntax in function calling <a href="http://docs.python.org/tutorial/controlflow.html#unpacking-argument-lists" rel="nofollow">"unpacks"</a> a dictionary into keyword arguments.</p> <p>Regardless it's sort of weird style. Here's what I would do instead:</p> <pre><code>for label in ['Cut', 'Copy', 'Paste', 'Delete']: editmenu.add_command(label=label, state='disabled') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload