使用 python 在 Tkinter 文本小部件中使用标签
我正在尝试在标签的帮助下以这种方式为 Tkinter 文本小部件中的文本着色:
text = self.text_field.get(1.0, 'end') #text_field is a text widget
s = re.findall("\d+", text)
for i in s:
self.text_field.tag_add(i, '1.0', 'end')
self.text_field.tag_configure(i, background='yellow',
font='helvetica 14 bold', relief='raised')
这个想法是所有标签都是动态创建的,因为我从文本小部件中获取数字,并且它们可以具有任意长度。此代码为小部件中的所有文本着色,但我只需要对数字进行着色。
有什么建议吗?
I'm trying color the text in the Tkinter text widget with help of tags in this way:
text = self.text_field.get(1.0, 'end') #text_field is a text widget
s = re.findall("\d+", text)
for i in s:
self.text_field.tag_add(i, '1.0', 'end')
self.text_field.tag_configure(i, background='yellow',
font='helvetica 14 bold', relief='raised')
The idea is that all tags are being dynamically created, because I get numbers from text widget and they can have any length. This code colors all text in the widget, but I need only numbers to be colored.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您这样做时,
您正在制作一个覆盖整个文本字段的标签。您只需使用正则表达式匹配的
.start()
和.stop()
方法在数字上添加文本。这里有一个进行语法突出显示的示例:
http://forums.devshed.com/python-programming-11 /syntax-highlighting-172406.html
When you do
You're making a tag that covers the whole text field. You need to just add the text on the numbers, using the
.start()
and.stop()
methods of the regex match.There's an example for doing syntax highlighting here:
http://forums.devshed.com/python-programming-11/syntax-highlighting-172406.html
类似问题的答案显示了如何扩展文本小部件以具有基于正则表达式突出显示文本的方法。有关示例,请参阅如何在 tkinter 文本小部件中突出显示文本
There is an answer to a similar question that shows how to extend the text widget to have a method that highlights text based on a regular expression. For examples see How to highlight text in a tkinter Text widget