How to partially underline Menu text in Tkinter when pressing shortcut keys?如何让 Tkinter 的 Menu 文本在按快捷键时部分加下划线?

简体中文

假设有这么一个程序,使用Tkinter创建了一个GUI页面,上面有一个菜单,我需要让这个菜单在检测到用户按下Alt键时,让菜单的文本变化(如:从原来的File(F)变为File(\underline{\text{F}}),就像IDLE中那样)。我该如何操作?非常感谢!

English

Assuming there is a program that uses Tkinter to create a GUI page with a menu, I need to make the menu text change (such as changing from File (F) to File (\underline {\text{F}}) when it detects the user pressing the Alt key, just like in IDLE). How should I proceed? Thank you So Much!

My English is not well,Sorry!

Does Tkinter expose a main loop, or expose a hook in it, into which a test for that key being pressed can be added?

Otheriwse I’d look at the source code and see how Idle does it.

In add_cascade calls, include this option: underline=char_index, where char_index is the 0-based index of the character you want indexed. For the top-level menu, at least on Windows, the underline only appears when Alt is pressed. (This is what IDLE does in Lib/idlelib/editor.py in createmenubar, line 480.) See this menu doc for more.

2 Likes

Can you provide a simple example? Thank you so much!
@tjreedy

Read the working code I referenced.