Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Tkinter menu item underline syntax [RESOLVED]

49 views
Skip to first unread message

Rich Shepard

unread,
Jan 6, 2021, 2:18:44 PM1/6/21
to
On Wed, 6 Jan 2021, 2QdxY4Rz...@potatochowder.com wrote:

> I'm not a TKinter expert (nor even a current user), but that line that
> begins with whitespace and "command =" looks suspicious. As far as I can
> see, Python is correct to call it syntactically erroneous.

Yes, the line above it needed to be terminated by a comma, not \n.

I found the answer: underline is an option and options are key: value pairs.
So, the correct syntax is underline: 0.

Thanks,

Rich

2QdxY4Rz...@potatochowder.com

unread,
Jan 6, 2021, 3:04:24 PM1/6/21
to
On 2021-01-06 at 11:18:15 -0800,
Glad you found it. :-)

For the sake of future generations who may run into this issue, can you
post the complete, correct call to file_menu.add_command?

Rich Shepard

unread,
Jan 6, 2021, 3:43:10 PM1/6/21
to
On Wed, 6 Jan 2021, 2QdxY4Rz...@potatochowder.com wrote:

> For the sake of future generations who may run into this issue, can you
> post the complete, correct call to file_menu.add_command?

This is the working version of the stanza I initially posted:

file_menu.add_command(
label = 'New',
command = self.callbacks['file->new', underline: 0],
accelerator = 'Ctrl+N'
)

The label line separated from the command with a comma. The underline option
is a key and separated from its value with a colon.

HTH,

Rich

Chris Angelico

unread,
Jan 6, 2021, 3:52:36 PM1/6/21
to
On Thu, Jan 7, 2021 at 7:43 AM Rich Shepard <rshe...@appl-ecosys.com> wrote:
> This is the working version of the stanza I initially posted:
>
> file_menu.add_command(
> label = 'New',
> command = self.callbacks['file->new', underline: 0],
> accelerator = 'Ctrl+N'
> )
>
> The label line separated from the command with a comma. The underline option
> is a key and separated from its value with a colon.
>

Are you sure that this works? It's syntactically valid, but I don't
think it means what you think it does.

ChrisA

Rich Shepard

unread,
Jan 6, 2021, 4:03:31 PM1/6/21
to
On Thu, 7 Jan 2021, Chris Angelico wrote:

> Are you sure that this works? It's syntactically valid, but I don't
> think it means what you think it does.

ChrisA,

I'm always open to learning. There's no error generated ... yet the
application doesn' open so it's time to run it through pdb.

Regards,

Rich

Chris Angelico

unread,
Jan 6, 2021, 4:30:17 PM1/6/21
to
Cool. Terry had something on the subject in the other thread; I think
that's a good place to start. (I don't know much about Tkinter, this
line of code just looked odd in general Python syntax.)

ChrisA

Rich Shepard

unread,
Jan 6, 2021, 5:10:16 PM1/6/21
to
On Thu, 7 Jan 2021, Chris Angelico wrote:

> Cool. Terry had something on the subject in the other thread; I think
> that's a good place to start. (I don't know much about Tkinter, this line
> of code just looked odd in general Python syntax.)

ChrisA,

It's been a long time since I wrote any Python code. Assignments have spaces
surrounding equal signs while values to options don't. I think I have the
code cleaned now.

Regards,

Rich

Terry Reedy

unread,
Jan 6, 2021, 5:22:00 PM1/6/21
to
On 1/6/2021 4:03 PM, Rich Shepard wrote:
> On Thu, 7 Jan 2021, Chris Angelico wrote:
>
>> Are you sure that this works? It's syntactically valid, but I don't
>> think it means what you think it does.
>
> ChrisA,
>
> I'm always open to learning. There's no error generated ... yet the
> application doesn' open so it's time to run it through pdb.

See my response, sent an hour ago, for how to use 'underline.


--
Terry Jan Reedy

Grant Edwards

unread,
Jan 6, 2021, 5:22:00 PM1/6/21
to
On 2021-01-06, Rich Shepard <rshe...@appl-ecosys.com> wrote:
> On Wed, 6 Jan 2021, 2QdxY4Rz...@potatochowder.com wrote:
>
>> For the sake of future generations who may run into this issue, can you
>> post the complete, correct call to file_menu.add_command?
>
> This is the working version of the stanza I initially posted:
>
> file_menu.add_command(
> label = 'New',
> command = self.callbacks['file->new', underline: 0],
> accelerator = 'Ctrl+N'
> )

I'm completely baffled by that. Can somebody explain how this
expression is evaluated?

self.callbacks['file->new', underline: 0]

It appears that the dict callbacks is being accessed with the key of
a tuple comprising a string and a slice.

Huh?

Chris Angelico

unread,
Jan 6, 2021, 5:28:25 PM1/6/21
to
Heh, you had to try it to make sure it was valid? Me too. Here's how
it's parsed:

self.callbacks[ # this bit's non-controversial
'file->new' # string literal
, # make tuple
underline: 0 # slice(underline, 0, None)
]

So it takes the name "underline", whatever that is, and it assumes you
want a slice from underline to zero. That's all the second dimension
of the indexing, where the first is a simple string literal.

ChrisA

Peter Otten

unread,
Jan 7, 2021, 5:27:20 AM1/7/21
to
On 06/01/2021 22:03, Rich Shepard wrote:
> On Thu, 7 Jan 2021, Chris Angelico wrote:
>
>> Are you sure that this works? It's syntactically valid, but I don't
>> think it means what you think it does.
>
> ChrisA,
>
> I'm always open to learning. There's no error generated ... yet the
> application doesn' open so it's time to run it through pdb.

Spoiler: unless the name 'underline' is defined you get a NameError:

NameError: name 'underline' is not defined

If it is defined and self.callbacks is a dict you get a TypeError:

TypeError: unhashable type: 'slice'

because that's what a colon means in a context like

x[a:b]

From what you posted I would guess that underline is most likely a
keyword argument to the add_command() method:

file_menu.add_command(
label='New',
command=self.callbacks['file->new'],
underline=0,
accelerator='Ctrl+N'
)

Peter Otten

unread,
Jan 7, 2021, 5:37:16 AM1/7/21
to
On 06/01/2021 22:03, Grant Edwards wrote:

> I'm completely baffled by that. Can somebody explain how this
> expression is evaluated?
>
> self.callbacks['file->new', underline: 0]
>
> It appears that the dict callbacks is being accessed with the key of
> a tuple comprising a string and a slice.
>
> Huh?

You're completely right:

>>> class C:
def __getitem__(self, index): return index


>>> c = C()
>>> underline = 42
>>> c["foo", underline: 0]
('foo', slice(42, 0, None))

The OP's code will most certainly fail at runtime ;)



Rich Shepard

unread,
Jan 7, 2021, 8:33:11 AM1/7/21
to
On Thu, 7 Jan 2021, Peter Otten wrote:

> Spoiler: unless the name 'underline' is defined you get a NameError:
> NameError: name 'underline' is not defined
> If it is defined and self.callbacks is a dict you get a TypeError:
> TypeError: unhashable type: 'slice'
> because that's what a colon means in a context like
> x[a:b]

The template I used for the menu doesn't scale to my application so I'm
re-writing it from scratch.

Thanks, all,

Rich

Terry Reedy

unread,
Jan 7, 2021, 10:08:05 AM1/7/21
to
On 1/6/2021 4:17 PM, Terry Reedy wrote:
> On 1/6/2021 4:03 PM, Rich Shepard wrote:
>> On Thu, 7 Jan 2021, Chris Angelico wrote:
>>
>>> Are you sure that this works? It's syntactically valid, but I don't
>>> think it means what you think it does.
>>
>> ChrisA,
>>
>> I'm always open to learning. There's no error generated ... yet the
>> application doesn' open so it's time to run it through pdb.
>
> See my response, sent an hour ago, for how to use 'underline.

Reposting:

'underline' has nothing to do with looking up the command in
self.callbacks. It is a keyword parameter for the add_command method,
and is handled like all other values passed by name, and as you did for
the other arguments

file_menu.add_command(
label='New',
underline=0,
command=self.callbacks['file->new],
accelerator='Ctrl+N'
)

Note: PEP 8 style is no spaces around '=' used for keyword arguments.
Here is an example from idlelib.editor, 978.

menu.add_command(label=ulchars[i] + " " + file_name,
command=callback,
underline=0)




--
Terry Jan Reedy

Rich Shepard

unread,
Jan 7, 2021, 10:56:36 AM1/7/21
to
On Wed, 6 Jan 2021, Terry Reedy wrote:

> 'underline' has nothing to do with looking up the command in
> self.callbacks. It is a keyword parameter for the add_command method, and
> is handled like all other values passed by name, and as you did for the
> other arguments
>
> file_menu.add_command(
> label='New',
> underline=0,
> command=self.callbacks['file->new],
> accelerator='Ctrl+N'
> )
>
> Note: PEP 8 style is no spaces around '=' used for keyword arguments. Here
> is an example from idlelib.editor, 978.
>
> menu.add_command(label=ulchars[i] + " " + file_name,
> command=callback,
> underline=0)

Thank you for the clarification.

Rich
0 new messages