在Tkinter中从文本小部件复制格式化的文本 [英] Copying formatted text from Text widget in tkinter

查看:132
本文介绍了在Tkinter中从文本小部件复制格式化的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tkinter开发Python中的APA引文生成器.生成引文后,我将使用文本"窗口小部件显示引文,但是每当我复制文本(此刻使用ctrl + c快捷键)时,其格式都会丢失.有什么方法可以从文本"小组件中复制格式化的文本(例如,斜体),而不是未格式化的文本?

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:

  1. 使用 text.dump(index1,index2,tag = True,text = True),可以从小部件中检索文本和标签.它返回一个类似的列表(这是下面示例中的小部件的内容):

  1. 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', '\n', '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中从文本小部件复制格式化的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆