Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Tkinter text Editing Control Text

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how to use the Tkinter text editing control Text. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use the Tkinter text editing control Text.

Unlike the Entry control, which supports only one line of input, the text editing control Text supports undo and redo as well as different formatting for different parts in addition to multi-line display and editing.

The Enbale button toggles whether the Text control allows editing by the user. Because the presentation color of the Text does not vary from state to state, the code also adjusts the representation color of the Text control.

# change state function.def change_state (): state= text.cget ('state') if state=='disabled': text.config (state='normal') text.config (background='#a0ffa0') else: text.config (state='disabled') text.config (background='#efefef') # change state button.eb = Button (root,text= "Enable", width=8, command=change_state) eb.grid (row=0, column=0, sticky=E+W)

The Delete button is used to delete the selection. It is important to note that the Text control gets the selection in a different way than Entry.

# delete selection.def delete_selection (): try: sel_from = text.index (SEL_FIRST) sel_to = text.index (SEL_LAST) # delete the selection. Text.delete (sel_from, sel_to) except TclError: pass

# delete selection button.db = Button (root,text= "Delete", width = 8, command=delete_selection) db.grid (row=0, column=1, sticky=E+W)

The Undo and Redo buttons directly use the Text method to achieve undo and redo functions. Lambda expressions are used in the code to simplify the call.

# undo buttonundo = Button (root, text='Undo', width = 8, command=lambda:text.edit_undo ()) undo.grid (row=0, column = 2, sticky=E+W) # redo buttonredo = Button (root, text='Redo', width = 8, command=lambda:text.edit_redo () redo.grid (row=0, column = 3, sticky=E+W)

There are four formatted buttons that share a set of processing logic, and each button calls the same format function with different parameters.

# create fontsfonts = [Font (family='SimHei', size=20, weight=BOLD), Font (family='SimHei', size=16), Font (family='SimSun', size=12, weight=BOLD), Font (family='SimSun', size=12)]

# delete selection.def format (index): tag_name = 'Format' + str (index) try: sel_from = text.index (SEL_FIRST) sel_to = text.index (SEL_LAST) for name in text.tag_names (): text.tag_remove (name, sel_from, sel_to) text.tag_add (tag_name, sel_from, sel_to) # set format at first time. Range_count = len (text.tag_ranges (tag_name)) if range_count = = 2: text.tag_config (tag_name, font=fonts [index]) except TclError: pass

# delete selection button.for i in range (0,4): fb = Button (root, text= "Format" + str (I), width = 8, command=lambda vicii: format (v)) fb.grid (row=1, column=i, sticky=E+W)

The code uses tag_remove, tag_add, tag_config and tag_names methods to achieve a simple format setting function.

Finally, the Text control is built. It is important to note that the value of the undo property is specified as True to make the undo and redo functionality effective.

# create text widget.text = Text (root, undo=True, background= "# a0ffa0", foreground= "# 000000", height = 10) text.grid (row=2, column=0, columnspan=8)

A regrettable fact is that the action of formatting the text is not included in the redo and undo processing.

At this point, I believe you have a deeper understanding of "how to use the Tkinter text editing control Text". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report