- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 689字
- 2021-06-24 18:35:08
Adding menu items
Next, we will add menu items in every individual menu. Not surprisingly, the code for the menu items needs to be added in the respective menu instance, as shown in the following screenshot:

In our example, we will add menu items to the File, Edit, and About menus (2.02.py).
The View menu has certain menu item variations, which will be tackled in the following section and are therefore not dealt with here.
Menu items are added by using the add_command() method. The format used to add menu items is as follows:
my_menu.add_command(label="Menu Item Label", accelerator='KeyBoard Shortcut', compound='left', image=my_image, underline=0, command=callback)
For example, you can create the Undo menu item by using the following syntax:
edit_menu.add_command(label="Undo", accelerator='Ctrl + Z', compound='left', image=undo_icon, command=undo_callback)
Some new menu-specific options that are introduced in the preceding code are as follows:
- accelerator: This option is used to specify a string, typically a keyboard shortcut, that can be used to invoke a menu. The string specified as an accelerator appears next to the text of the menu item. Please note that this does not automatically create bindings for the keyboard shortcut. We will have to manually set them up. This will be discussed later.
- compound: Specifying a compound option for a menu item lets you add images beside a menu label. A specification such as compound='left', label= 'Cut', image=cut_icon means that the cut icon will appear to the left of the Cut menu label. The icons that we will use here are stored and referenced in a separate folder called icons.
- underline: The underline option lets you specify the index of a character in the menu text that needs to be underlined. The indexing starts at 0, which means that specifying underline=1 underlines the second character of the text. Besides underlining, Tkinter also uses it to define the default bindings for the keyboard traversal of menus. This means that we can select the menu either with the mouse pointer or with the Alt + <character_at_the_underlined_index> shortcut.
To add the New menu item in the File menu, use the following code:
file_menu.add_command(label="New", accelerator='Ctrl+N', compound='left', image=new_file_icon, underline=0, command=new_file)
Occasionally, in menu items, you will come across code such as my_menu.add_separator(). This widget displays a separator bar and is solely used to organize similar menu items in groups, separating groups by horizontal bars.
Next, we will add a Frame widget to hold the shortcut icons. We will also add a Text widget to the left to display line numbers, as shown in the following screenshot (2.02.py):

Having reserved the space, we can later add shortcut icons or line numbers and keep the Frame widget as the parent widget. Adding frames is easy; we have done that in the past. The code is as follows (refer to 2.02.py):
shortcut_bar = Frame(root, height=25, background='light sea green')
shortcut_bar.pack(expand='no', fill='x')
line_number_bar = Text(root, width=4, padx=3, takefocus=0, border=0, background='khaki', state='disabled', wrap='none')
line_number_bar.pack(side='left', fill='y')
We have applied a background color to these two widgets, for now, to differentiate them from the body of the Toplevel window.
Lastly, let's add the main Text widget and the Scrollbar widget, as follows (2.02.py):
content_text = Text(root, wrap='word')
content_text.pack(expand='yes', fill='both')
scroll_bar = Scrollbar(content_text)
content_text.configure(yscrollcommand=scroll_bar.set)
scroll_bar.config(command=content_text.yview)
scroll_bar.pack(side='right', fill='y')
The code is similar to how we instantiated all the other widgets so far. However, note that the scrollbar is configured to yview of the Text widget, and the Text widget is configured to connect to the Scrollbar widget. This way, the widgets are cross-connected to each other.
Now, when you scroll down the Text widget, the scrollbar reacts to it. Alternatively, when you move the scrollbar, the Text widget reacts in turn.
- Mastering Ext JS(Second Edition)
- HTML5+CSS3王者歸來
- Mastering Visual Studio 2017
- 從程序員到架構師:大數據量、緩存、高并發、微服務、多團隊協同等核心場景實戰
- Spring Boot+Spring Cloud+Vue+Element項目實戰:手把手教你開發權限管理系統
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- CKA/CKAD應試教程:從Docker到Kubernetes完全攻略
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Clojure Reactive Programming
- 學習OpenCV 4:基于Python的算法實戰
- RESTful Java Web Services(Second Edition)
- 響應式架構:消息模式Actor實現與Scala、Akka應用集成
- C++ Fundamentals
- 代碼閱讀
- Python硬件編程實戰