在Tk中,可以创建显示相同内容的文本小部件的对等体。也可以将对等体的字体设置为与原始对等体的字体大小不同的字体。
这很有效,直到我想要将部分文本设置为粗体(在原始小部件中,但同级文本是否也加粗并不重要)。就我所知,通过查看text(3tk),除了创建一个加粗字体的标记之外,没有其他方法可以加粗文本的一部分。但该字体随后也会在对等体中使用,包括字体的大小。
import tkinter
root = tkinter.Tk()
text = tkinter.Text(root)
text.pack(side='left', fill='both', expand=True)
text.insert('end', 'hello world\n' * 10)
# Problem: the font size of the tag applies in both text widgets
text.tag_config('foo', font=('Monospace', 12, 'bold'))
text.tag_add('foo', '2.2', '2.7')
peer = tkinter.Text(root)
peer.destroy() # the underlying tk widget gets replaced with the peer
text.peer_create(peer, font=('Monospace', 3, ''))
peer.pack(side='left', fill='y')
root.mainloop()在两个文本小部件中显示不同大小的粗体文本时,如何将文本的某些部分加粗?

发布于 2020-05-01 22:51:27
有一个标签只适用于其中一个对等体。它是sel,表示被选中的文本。这是可行的:
peer.tag_config('sel', font=('Monospace', 3, ''))
peer.tag_add('sel', '1.0', 'end')一个很大的缺点是,当粗体部分被取消选择时,字体大小会跳回通常的大小。如果您不需要选择其他任何内容,则可以强制始终选择所有文本。
https://stackoverflow.com/questions/61489218
复制相似问题