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

The icons toolbar and View menu functions

In this iteration, we will add the following functionalities to the text editor:

  • Showing shortcut icons on the toolbar
  • Displaying line numbers
  • Highlighting the current line
  • Changing the color theme of the editor

Let's start with a simple task first. In this step, we will add shortcut icons to the toolbar, as shown in the following screenshot:

You may recall that we have already created a frame to hold these icons. Let's add these icons now.

While adding these icons, we have followed a convention. The icons have been named exactly the same as the corresponding function that handles them. Following this convention has enabled us to loop through a list, simultaneously apply the icon image to each button, and add the command callback from within the loop. All the icons have been placed in the icons folder.

The following code adds icons to the toolbar (2.10.py):

icons = ('new_file', 'open_file', 'save', 'cut', 'copy', 'paste', 'undo', 'redo', 'find_text')
for i, icon in enumerate(icons):
tool_bar_icon = PhotoImage(file='icons/{}.gif'.format(icon))
cmd = eval(icon)
tool_bar = Button(shortcut_bar, image=tool_bar_icon, command=cmd)
tool_bar.image = tool_bar_icon
tool_bar.pack(side='left')

The following is a description of the preceding code:

  • We have already created a shortcut bar in the first iteration. Now, we will simply add buttons with images in the frame.
  • We create a list of icons, taking care to name them exactly the same as the name of the icons.
  • We then loop through the list by creating a Button widget, adding an image to the button, and adding the respective callback command.
  • Before adding the callback command, we have to convert the string to an equivalent expression by using the eval command. If we do not apply eval, it cannot be applied as an expression to the callback command.

Thus, we've added shortcut icons to the shortcut bar. Now, if you run the code (refer to 2.10.py in the code bundle), it should show all of the shortcut icons. Moreover, as we have linked each button to its callback, all of these shortcut icons should work.

主站蜘蛛池模板: 卫辉市| 志丹县| 喀喇沁旗| 新建县| 盈江县| 昭平县| 福清市| 专栏| 泾源县| 铜川市| 遂溪县| 吉木乃县| 上饶县| 惠东县| 定边县| 泰州市| 白河县| 莱芜市| 昭通市| 贺兰县| 崇明县| 广昌县| 威远县| 二手房| 望都县| 乌拉特前旗| 东明县| 彭阳县| 临猗县| 丰镇市| 万全县| 刚察县| 礼泉县| 布拖县| 绥阳县| 石棉县| 钦州市| 泗水县| 盐城市| 凤翔县| 藁城市|