<small id='hzJLO'></small><noframes id='hzJLO'>

      • <bdo id='hzJLO'></bdo><ul id='hzJLO'></ul>
    1. <tfoot id='hzJLO'></tfoot>
      <i id='hzJLO'><tr id='hzJLO'><dt id='hzJLO'><q id='hzJLO'><span id='hzJLO'><b id='hzJLO'><form id='hzJLO'><ins id='hzJLO'></ins><ul id='hzJLO'></ul><sub id='hzJLO'></sub></form><legend id='hzJLO'></legend><bdo id='hzJLO'><pre id='hzJLO'><center id='hzJLO'></center></pre></bdo></b><th id='hzJLO'></th></span></q></dt></tr></i><div id='hzJLO'><tfoot id='hzJLO'></tfoot><dl id='hzJLO'><fieldset id='hzJLO'></fieldset></dl></div>
    2. <legend id='hzJLO'><style id='hzJLO'><dir id='hzJLO'><q id='hzJLO'></q></dir></style></legend>

      从 tkinter 中的 Text 小部件复制格式化文本

      时间:2023-09-03
        <tbody id='B2xQH'></tbody>
      <legend id='B2xQH'><style id='B2xQH'><dir id='B2xQH'><q id='B2xQH'></q></dir></style></legend>
        <bdo id='B2xQH'></bdo><ul id='B2xQH'></ul>

            1. <small id='B2xQH'></small><noframes id='B2xQH'>

              <i id='B2xQH'><tr id='B2xQH'><dt id='B2xQH'><q id='B2xQH'><span id='B2xQH'><b id='B2xQH'><form id='B2xQH'><ins id='B2xQH'></ins><ul id='B2xQH'></ul><sub id='B2xQH'></sub></form><legend id='B2xQH'></legend><bdo id='B2xQH'><pre id='B2xQH'><center id='B2xQH'></center></pre></bdo></b><th id='B2xQH'></th></span></q></dt></tr></i><div id='B2xQH'><tfoot id='B2xQH'></tfoot><dl id='B2xQH'><fieldset id='B2xQH'></fieldset></dl></div>

              • <tfoot id='B2xQH'></tfoot>
                本文介绍了从 tkinter 中的 Text 小部件复制格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 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:

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

                上一篇:Jupyter Notebook 小部件:创建相关下拉列表 下一篇:如何在交互式绘图(Python)中用鼠标指向(x,y)位置?

                相关文章

                • <bdo id='2sDLo'></bdo><ul id='2sDLo'></ul>

                <tfoot id='2sDLo'></tfoot>

                    <small id='2sDLo'></small><noframes id='2sDLo'>

                    <i id='2sDLo'><tr id='2sDLo'><dt id='2sDLo'><q id='2sDLo'><span id='2sDLo'><b id='2sDLo'><form id='2sDLo'><ins id='2sDLo'></ins><ul id='2sDLo'></ul><sub id='2sDLo'></sub></form><legend id='2sDLo'></legend><bdo id='2sDLo'><pre id='2sDLo'><center id='2sDLo'></center></pre></bdo></b><th id='2sDLo'></th></span></q></dt></tr></i><div id='2sDLo'><tfoot id='2sDLo'></tfoot><dl id='2sDLo'><fieldset id='2sDLo'></fieldset></dl></div>
                  1. <legend id='2sDLo'><style id='2sDLo'><dir id='2sDLo'><q id='2sDLo'></q></dir></style></legend>