2016-05-02 45 views
0

通常のテキストをファンキーなテキストに変換するTkinterを使用して、Pythonでプログラムを作成しました。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() 

が、それはのpythonに来るとき、私はまだかなりのnoobだ、大規模な非効率性を言い訳してください:

は、ここに私のコードです。

答えて

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(出力)はここにあなたのケースのための

を動作するはずです
関連する問題