官术网_书友最值得收藏!

Adding a menu and menu items

Menus offer a very compact way of presenting a large number of choices to the user without cluttering the interface. Tkinter offers the following two widgets to handle menus:

  • Menu widget: This appears at the top of applications, which is always visible to end users
  • Menu items: These show up when a user clicks on a menu 

We will use the following code to add Toplevel menu buttons:

my_menu = Menu(parent, **options)

For example, to add a File menu, we will use the following code:

# Adding Menubar in the widget
menu_bar = Menu(root)
file_menu = Menu(menu_bar, tearoff=0)
# all file menu-items will be added here next
menu_bar.add_cascade(label='File', menu=file_menu)
root.config(menu=menu_bar)

The following screenshot is the result of the preceding code (2.01.py):

Similarly, we will add the Edit, View, and About menus (2.01.py).

We will also define a constant as follows:

PROGRAM_NAME = " Footprint Editor "

Then, we'll set the root window tile, as follows:

root.title(PROGRAM_NAME)

Most Linux platforms support tear-off menus. When tearoff is set to 1 (enabled), the menu appears with a dotted line above the menu options. Clicking on the dotted line enables the user to literally tear off or separate the menu from the top. However, as this is not a cross-platform feature, we have decided to disable tear-off, marking it as tearoff = 0.

主站蜘蛛池模板: 慈利县| 吉木乃县| 黑龙江省| 凤翔县| 塘沽区| 游戏| 宽甸| 石棉县| 湟源县| 偃师市| 天长市| 灵寿县| 历史| 克拉玛依市| 宜兰市| 江门市| 贺州市| 溧阳市| 吉木乃县| 苍南县| 仁布县| 赣州市| 冀州市| 肥西县| 太白县| 怀来县| 恩施市| 裕民县| 保康县| 甘泉县| 泽普县| 乡城县| 洛南县| 宜兰市| 泾阳县| 米脂县| 甘德县| 延吉市| 曲阳县| 华亭县| 满城县|