|
17 | 17 | - 'end' refers to the position after the last character of a line
|
18 | 18 | - Please refer the indices section of tkinter documentation of Text Widget for more details
|
19 | 19 |
|
| 20 | +Tags can be created in text widget to modify words/text at various locations with some logic. |
| 21 | +Tags are mapped to the specified locations/indices of text in Text widget. |
| 22 | +So it is possible to use these tags for text modification instead of text indices. |
| 23 | +Therefore, making it convenient for programming. |
| 24 | +
|
| 25 | +
|
20 | 26 | '''
|
21 | 27 |
|
22 | 28 | importtkinterastk
|
@@ -58,10 +64,49 @@ def __init__(self, master):
|
58 | 64 | self.btn8=ttk.Button(self.master,text='Disable Text Field',command=self.disableEnable)
|
59 | 65 | self.btn8.pack()
|
60 | 66 |
|
61 |
| - |
62 | 67 | self.lbl1=ttk.Label(self.master,text='Waiting to display TEXT....')
|
63 | 68 | self.lbl1.pack()
|
64 |
| - |
| 69 | + |
| 70 | + |
| 71 | +self.btn9=ttk.Button(self.master,text='Create tag named\'myTag\' at line 2',command=self.tagDemo) |
| 72 | +self.btn9.pack() |
| 73 | + |
| 74 | +deftagDemo(self): |
| 75 | +ifself.btn9['text']=='Create tag named\'myTag\' at line 2': |
| 76 | +self.btn9.config(text='Remove Tag') |
| 77 | +self.text.tag_add('myTag','2.0','2.0 lineend') |
| 78 | + |
| 79 | +self.btn10=ttk.Button(self.master,text='Change myTag background to yellow',command=self.tagbgyellow) |
| 80 | +self.btn10.pack() |
| 81 | + |
| 82 | +self.btn11=ttk.Button(self.master,text='Remove tag from 1st word of line 2',command=self.tagrm21word) |
| 83 | +self.btn11.pack() |
| 84 | + |
| 85 | +self.btn12=ttk.Button(self.master,text='myTag Span',command=self.getTagSpan) |
| 86 | +self.btn12.pack() |
| 87 | + |
| 88 | +self.btn13=ttk.Button(self.master,text='Show all Tags in Text widget',command=self.displayAllTags) |
| 89 | +self.btn13.pack() |
| 90 | + |
| 91 | +else: |
| 92 | +self.btn9.config(text='Create tag named\'myTag\' at line 2') |
| 93 | +self.text.tag_delete('myTag') |
| 94 | +self.btn10.destroy() |
| 95 | +self.btn11.destroy() |
| 96 | +self.btn12.destroy() |
| 97 | +self.btn13.destroy() |
| 98 | + |
| 99 | +defgetTagSpan(self): |
| 100 | +self.lbl1.config(text=self.text.tag_ranges('myTag')) |
| 101 | + |
| 102 | +defdisplayAllTags(self): |
| 103 | +self.lbl1.config(text=self.text.tag_names()) |
| 104 | + |
| 105 | +deftagrm21word(self): |
| 106 | +self.text.tag_remove('myTag','2.0','2.0 wordend') |
| 107 | +deftagbgyellow(self): |
| 108 | +self.text.tag_configure('myTag',background='yellow') |
| 109 | + |
65 | 110 | defrandomTextEntry(self):
|
66 | 111 | text='''Random Text
|
67 | 112 | It real sent your at.
|
|