私が書いたテキストエディタの標準コピー&のペースト機能を実装しましたが、tkinterのスクロールテキストウィジェットの挿入メソッドの動作を正しく理解していないようです:私のコードは、 。python、tkinter:テキストはクリップボードから2回貼り付けられます - なぜですか?
ここでは最小の例です。これは、スクロールテキストウィジェットと、テキストウィジェットに "Test.txt"というサンプルファイルをロードするボタンと終了ボタンを備えたウィンドウです。コピー&の貼り付け機能はショートカットctrl-c、ctrl-vで実装されています。ペーストすると、コードはクリップボードの内容を2回インスツルして挿入します。私が間違ったことを誰かに見せてもらえますか?前もって感謝します!
import tkinter, tkinter.scrolledtext
def ende():
main.destroy()
def loadSampleFile():
d=open("Test.txt")
z=d.readline()
while z:
t.insert("end",z)
z=d.readline()
d.close()
def paste(event_obj):
text2paste=t.selection_get(selection='CLIPBOARD')
print(t.clipboard_get())
t.insert('insert',text2paste)
def copy2clipboard(event_obj):
text2copy=t.get(tkinter.SEL_FIRST,tkinter.SEL_LAST)
t.clipboard_clear()
t.clipboard_append(text2copy)
main=tkinter.Tk()
t=tkinter.scrolledtext.ScrolledText(main, width=40, height=1)
t.pack()
t.bind('<Control-c>',copy2clipboard)
t.bind('<Control-v>',paste)
bshow=tkinter.Button(main, text="Show File", command=loadSampleFile)
bshow.pack()
bende=tkinter.Button(main, text="end", command=ende)
bende.pack()
main.mainloop()