2016-05-02 34 views
0

我已經在python中使用Tkinter創建了一個程序,將普通文本轉換爲一些奇特的文本。自動將Tkinter文本小部件內容複製到剪貼板?

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() 

請原諒大量的低效率,我還是相當多的小白,當談到蟒蛇。

回答

2

此鏈接可以幫助您:

Link

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

謝謝。我不確定r.destroy()或者我的情況win.destroy()是做什麼的,但它關閉了窗口。我刪除了那部分,它完美地工作。再次感謝! –

+0

**沒問題:D **你可以標記爲答案plz! –

+0

這真的很有幫助,你不知道讓用戶在Mac上用python 3手動複製東西到剪貼板是多麼的困難 – madprogramer

0

下面是我在我的一個應用程序中使用的一個小函數,我將其附加到tkinter按鈕。您需要將「INFO_TO_COPY」註釋更改爲您的實際數據源。

def copy_button(): 
    clip = Tk() 
    clip.withdraw() 
    clip.clipboard_clear() 
    clip.clipboard_append(INFO_TO_COPY) // Change INFO_TO_COPY to the name of your data source 
    clip.destroy() 

希望這有助於!

0

我從任何類型的文件之外,但如果我沒有記錯win.clipboard_append(輸出)應該爲你的情況下,在這裏工作