问题描述
我正在使用 tkinter 在 Python 中开发一个 APA 引用生成器.我使用 Text 小部件在生成引文后显示引文,但每当我复制文本(目前使用 ctrl+c 快捷方式)时,它就会丢失其格式.有什么方法可以从 Text 小部件复制格式化文本(例如斜体)而不是无格式文本?
I'm working on an APA citation maker in Python using tkinter. I use the Text widget to display the citation once it's generated, but whenever I copy the text (using the ctrl+c shortcut, at the moment) it loses its formatting. Is there some way to copy formatted text (italicized, for instance) from the Text widget rather than unformatted text?
推荐答案
要将格式化文本复制到剪贴板,需要python和系统剪贴板之间的接口,该接口支持文本格式化.我发现 klembord 应该适用于 Linux 和 Windows(Mac 用户可能可以将以下解决方案调整为richxerox).
To copy formatted text to the clipboard, you need an interface between python and the system's clipboard which supports text formatting. I have found klembord which should works in Linux and Windows (Mac users can probably adapt the below solution to richxerox).
想法是 (1) 将格式化文本从文本小部件转换为 html,然后 (2) 将其添加到剪贴板:
The idea is to (1) convert the formatted text from the text widget to html then to (2) add it to the clipboard:
使用
text.dump(index1, index2, tag=True, text=True)
可以从小部件中检索文本和标签.它返回一个类似的列表(这是下面示例中小部件的内容):
With
text.dump(index1, index2, tag=True, text=True)
it is possible to retrieve the text and tags from the widget. It returns a list like (this is the content of the widget in the example below):
[('text', 'Author et al. (2012). The title of the article. ', '1.0'),
('tagon', 'italic', '1.48'),
('text', 'Journal Name', '1.48'),
('tagoff', 'italic', '1.60'),
('text', ', ', '1.60'),
('tagon', 'bold', '1.62'),
('text', '2', '1.62'),
('tagoff', 'bold', '1.63'),
('text', '(599), 1–5.', '1.63'),
('text', '
', '1.74')]
因此很容易使用字典将每个 ('tagon/off', tagname)
对与相应的 html 标签关联起来,并将小部件内容转换为 html.
So it is easy to associate each ('tagon/off', tagname)
pair with the corresponding html tag using a dictionary and convert the widget content to html.
klembord.set_with_rich_text(txt, rich_txt)
将字符串 txt
及其 html 格式的等价物放入剪贴板.
klembord.set_with_rich_text(txt, rich_txt)
puts the string txt
and its html formatted equivalent in the clipboard.
这是一个完整的示例(在 Linux 中测试,我能够从文本小部件中复制文本并将其粘贴到带有格式的文字处理器中):
Here is a full example (tested in Linux, I was able to copy the text from the text widget and paste it into a word processor with the formatting):
import tkinter as tk
import klembord
root = tk.Tk()
text = tk.Text(root)
text.pack(fill='both', expand=True)
text.tag_configure('italic', font='TkDefaultFont 9 italic')
text.tag_configure('bold', font='TkDefaultFont 9 bold')
TAG_TO_HTML = {
('tagon', 'italic'): '<i>',
('tagon', 'bold'): '<b>',
('tagoff', 'italic'): '</i>',
('tagoff', 'bold'): '</b>',
}
def copy_rich_text(event):
try:
txt = text.get('sel.first', 'sel.last')
except tk.TclError:
# no selection
return "break"
content = text.dump('sel.first', 'sel.last', tag=True, text=True)
html_text = []
for key, value, index in content:
if key == "text":
html_text.append(value)
else:
html_text.append(TAG_TO_HTML.get((key, value), ''))
klembord.set_with_rich_text(txt, ''.join(html_text))
return "break" # prevent class binding to be triggered
text.bind('<Control-c>', copy_rich_text)
text.insert("1.0", "Author et al. (2012). The title of the article. ")
text.insert("end", "Journal Name", "italic")
text.insert("end", ", ")
text.insert("end", "2", "bold")
text.insert("end", "(599), 1–5.")
root.mainloop()
这篇关于从 tkinter 中的 Text 小部件复制格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!