当每个标签向文本添加样式时,处理文本小部件中的多个标签吗?
我想为文本小部件创建一个工具栏,允许您选择粗体或斜体文本以及它们的组合(粗体和斜体在一起)。
我一直在查看对 高级 Tkinter 文本框 的响应,以及StyledText 小部件,但都没有回答我的问题。
配置标签的基本方法:
txtWidget.tag_configure("bold", font=my_bold_font)
txtWidget.tag_configure("italic", font=my_italic_font)
这意味着您可以将单个字体与单个标签相关联,但似乎没有任何方法(表面上)将标签组合与字体相关联。
继续创建 tag_bind 事件的最佳方法是找出我的标签组合并相应地分配字体的方法,还是有其他方法?
I want to create a toolbar for a text widget that allows you to select bold or italic text, as well as their combination (bold and italic together).
I've been looking at this response to Advanced Tkinter text box, as well as code for the StyledText widget, but neither answers my question.
The basic way to configure tags:
txtWidget.tag_configure("bold", font=my_bold_font)
txtWidget.tag_configure("italic", font=my_italic_font)
It means you can associate a single font with a single tag, but there doesn't appear to be any way (on the surface) to associate tag combinations with a font.
Is the best way to proceed to create a tag_bind event to a method that will figure out my tag combinations and assign the font accordingly, or is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遗憾的是,这是文本小部件的少数弱点之一。如果您想要粗体和斜体,则需要一个具有这两个属性的字体的标签。您可以动态创建新标签,因此这不是一个不可能解决的问题,但确定范围中的哪些字符需要“粗体”标签、哪些需要“斜体”标签以及哪些字符可能有点乏味。新的“bold_italic”标签。
Sadly, this is one of the few weaknesses of the text widget. If you want both bold and italic you need a tag with a font that has both of those attributes. You can create new tags on the fly so it's not an impossible problem to solve, but it can be a bit tedious to figure out which characters in a range need the "bold" tag, which need the "italic" tag, and which needs a new "bold_italic" tag.