इन उद्देश्यों के लिए उपयोग करने का यह सही विजेट है। मूल अवधारणा यह है कि, आप टैग को गुण निर्दिष्ट करते हैं, और आप विजेट में टेक्स्ट की श्रेणियों पर टैग लागू करते हैं। आप टेक्स्ट पैटर्न के search
कमांड का उपयोग अपने पैटर्न से मेल खाने वाले तारों को ढूंढने के लिए कर सकते हैं, जो आपको पर्याप्त जानकारी लौटाएंगे जो मिलान की सीमा तक एक टैग लागू करते हैं।
पाठ पर टैग कैसे लागू करें के उदाहरण के लिए, Advanced Tkinter text box? पर मेरा उत्तर देखें। यह वही नहीं है जो आप करना चाहते हैं लेकिन यह मूल अवधारणा को दिखाता है।
एक उदाहरण है कि आप टेक्स्ट क्लास को कैसे बढ़ा सकते हैं ताकि एक पैटर्न से मेल खाने वाले टेक्स्ट को हाइलाइट करने के लिए विधि शामिल हो।
इस कोड में पैटर्न एक स्ट्रिंग होना चाहिए, यह एक संकलित नियमित अभिव्यक्ति नहीं हो सकता है। साथ ही, पैटर्न को Tcl's syntax rules for regular expressions का पालन करना होगा।
class CustomText(tk.Text):
'''A text widget with a new method, highlight_pattern()
example:
text = CustomText()
text.tag_configure("red", foreground="#ff0000")
text.highlight_pattern("this should be red", "red")
The highlight_pattern method is a simplified python
version of the tcl code at http://wiki.tcl.tk/3246
'''
def __init__(self, *args, **kwargs):
tk.Text.__init__(self, *args, **kwargs)
def highlight_pattern(self, pattern, tag, start="1.0", end="end",
regexp=False):
'''Apply the given tag to all text that matches the given pattern
If 'regexp' is set to True, pattern will be treated as a regular
expression according to Tcl's regular expression syntax.
'''
start = self.index(start)
end = self.index(end)
self.mark_set("matchStart", start)
self.mark_set("matchEnd", start)
self.mark_set("searchLimit", end)
count = tk.IntVar()
while True:
index = self.search(pattern, "matchEnd","searchLimit",
count=count, regexp=regexp)
if index == "": break
if count.get() == 0: break # degenerate pattern which matches zero-length strings
self.mark_set("matchStart", index)
self.mark_set("matchEnd", "%s+%sc" % (index, count.get()))
self.tag_add(tag, "matchStart", "matchEnd")
यह सही विजेट है। देखें कि ['निष्क्रिय '] (http://hg.python.org/cpython/file/63a00d019bb2/Lib/idlelib) करता है। – tzot
@tzot आप कम से कम एक बेहतर संकेत दे सकते हैं कि किसी को कौन सी फाइलें देखना चाहिए। 'idlelib' में कई फाइलें और मॉड्यूल इत्यादि शामिल हैं, और मेरी राय में, वास्तविक दस्तावेज के बिना, और अधिकतर यदि किसी के पास अधिक अनुभव नहीं है, तो कुछ ढूंढना मुश्किल है। मैं इस वेबसाइट के उपयोगकर्ताओं को पहले इस आलेख में ले जाऊंगा: https://docs.python.org/3.5/library/idle.html – nbro