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

Rya*_*yan 1 python copy-paste tkinter

我使用 Tkinter 在 python 中创建了一个程序,将普通文本转换为一些花哨的文本。

截屏

我想知道是否有可能在按下“转换”按钮后将输出自动复制到剪贴板。

这是我的代码:

#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": "?",
        "B": "?",
        "C": "?",
        "D": "?",
        "E": "?",
        "F": "?",
        "G": "?",
        "H": "?",
        "I": "?",
        "J": "?",
        "K": "?",
        "L": "?",
        "M": "?",
        "N": "?",
        "O": "?",
        "P": "?",
        "Q": "?",
        "R": "?",
        "S": "?",
        "T": "?",
        "U": "?",
        "V": "?",
        "W": "?",
        "X": "?",
        "Y": "?",
        "Z": "?",

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

        #Numbers
        "1": "?",
        "2": "?",
        "3": "?",
        "4": "?",
        "5": "?",
        "6": "?",
        "7": "?",
        "8": "?",
        "9": "?",
        "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()
Run Code Online (Sandbox Code Playgroud)

请原谅巨大的低效率,当谈到 python 时,我仍然是一个菜鸟。

小智 8

此链接可以帮助您:

关联

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.destroy()
Run Code Online (Sandbox Code Playgroud)