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

Implementing the Select All feature

We know that Tkinter has a built-in sel tag that applies a selection to a given text range. We want to apply this tag to the entire text in the widget.

We can simply define a function to handle this, as follows (2.05.py):

def select_all(event=None):
content_text.tag_add('sel', '1.0', 'end')
return "break"

After doing this, add a callback to the Select All menu item:

edit_menu.add_command(label='Select All', underline=7, accelerator='Ctrl+A', command=select_all)

We also need to bind the function to the Ctrl + A keyboard shortcut. We do this by using the following key bindings (2.05.py):

content_text.bind('<Control-A>', select_all)
content_text.bind('<Control-a>', select_all)

The coding of the Select All feature is complete. To try it out, add some text to the text widget and then click on the menu item, Select All, or use Ctrl + A (accelerator shortcut key).

主站蜘蛛池模板: 凉山| 长宁县| 新兴县| 罗定市| 广南县| 葫芦岛市| 连南| 江孜县| 兰西县| 仙桃市| 白沙| 房产| 定日县| 芷江| 通州区| 朔州市| 扎鲁特旗| 济阳县| 邹城市| 乃东县| 株洲县| 洛南县| 巫山县| 兖州市| 池州市| 克东县| 兴城市| 陇南市| 贡觉县| 清远市| 新密市| 二连浩特市| 尉氏县| 华阴市| 如皋市| 花莲县| 井冈山市| 连云港市| 香格里拉县| 来凤县| 湾仔区|