自动将Tkinter文本小部件内容复制到剪贴板?

时间:2016-05-02 19:41:19

标签: python tkinter copy-paste

我使用Tkinter在python中创建了一个程序,它将普通文本转换成一些奇特的文本。

screenshot

我想知道输出是否可以在转换后自动复制到剪贴板。按钮已被按下。

这是我的代码:

#Importing TKinter module
from tkinter import *

#Setting up the GUI window
win = Tk()
win.title("Font Converter")
win.resizable(0,0)

#Converting
def replace():
    text = entry.get("1.0",END)

    replacements = {

        #Upper case letters
        "A": "A",
        "B": "B",
        "C": "C",
        "D": "D",
        "E": "E",
        "F": "F",
        "G": "G",
        "H": "H",
        "I": "I",
        "J": "J",
        "K": "K",
        "L": "L",
        "M": "M",
        "N": "N",
        "O": "O",
        "P": "P",
        "Q": "Q",
        "R": "R",
        "S": "S",
        "T": "T",
        "U": "U",
        "V": "V",
        "W": "W",
        "X": "X",
        "Y": "Y",
        "Z": "Z",

        #Lower case letters
        "a": "a",
        "b": "b",
        "c": "c",
        "d": "d",
        "e": "e",
        "f": "f",
        "g": "g",
        "h": "h",
        "i": "i",
        "j": "j",
        "k": "k",
        "l": "l",
        "m": "m",
        "n": "n",
        "o": "o",
        "p": "p",
        "q": "q",
        "r": "r",
        "s": "s",
        "t": "t",
        "u": "u",
        "v": "v",
        "w": "w",
        "x": "x",
        "y": "y",
        "z": "z",

        #Numbers
        "1": "1",
        "2": "2",
        "3": "3",
        "4": "4",
        "5": "5",
        "6": "6",
        "7": "7",
        "8": "8",
        "9": "9",
        "0": "0",

    }
    text = "".join([replacements.get(c, c) for c in text])
    output.delete('1.0', END)
    output.insert(END, str(text))

#Text Variables
enter = StringVar()

#Creating the widgets
l1 = Label(win, text="Enter text:")
entry = Text(win, width=50, height=3, wrap=WORD)
button = Button(win, text="Convert", width=20)
l2 = Label(win, text="Converted text:")
output = Text(win, width=50, height=3, wrap=WORD)

#Positioning the widgets
l1.grid(row=1, column=1, padx=5, sticky=W)
entry.grid(row=2, column=1, columnspan=2, padx=5, pady=(0,10))
button.grid(row=3, column=1, columnspan=2, pady=5)
l2.grid(row=4, column=1, padx=5, sticky=W)
output.grid(row=5, column=1, columnspan=2, padx=5, pady=(0,10))

#Button activation
button.configure(command=replace)

#So the program is on repeat
win.mainloop()

请原谅大规模的低效率,当涉及到python时,我仍然是一个非常棒的小说。

3 个答案:

答案 0 :(得分:2)

此链接可以帮助您:

Link

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()

答案 1 :(得分:0)

这是我在我的一个应用程序中使用的一个小功能,我将其附加到tkinter按钮。您需要更改" INFO_TO_COPY"评论您的实际数据来源。

var x = 1;
$(add_button).click(function(e){
    e.preventDefault();
    if(x < max_fields){
        x++;
        $(wrapper).append('
<input type="text" name="name" id="name"/>
<input type="radio" name="attend" id="attendC" value="cere" />
<input type="radio" name="attend" id="attendR" value="rece" />
<input type="radio" name="attend" id="attendB" value="both" />
<input type="radio" name="attend" id="attendN" value="neit" />
');
}
});

希望这有帮助!

答案 2 :(得分:0)

我远离任何类型的文档,但如果我没记错,win.clipboard_append(输出)应该适用于你的案例