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

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).

主站蜘蛛池模板: 桃源县| 荣成市| 保靖县| 瑞丽市| 沭阳县| 惠东县| 凌源市| 乳山市| 桂平市| 夏邑县| 马边| 茶陵县| 剑河县| 商南县| 龙陵县| 新乡县| 咸宁市| 金山区| 桃园市| 蒙城县| 万全县| 内江市| 来安县| 锦屏县| 西贡区| 乐平市| 报价| 黔西县| 乌审旗| 大厂| 武胜县| 信宜市| 田东县| 惠来县| 浠水县| 蒙城县| 神木县| 德兴市| 霍邱县| 焉耆| 龙山县|