2

私はプログラミングの初心者です。私は趣味としてこれを行い、仕事の生産性を向上させます。

entryユーザーがテキスト行をコピーするたびに、クリップボードを Tkinter に自動的に貼り付けるプログラムを作成しています。

ループを使用whileして現在のクリップボードに変更があるかどうかを検出し、新しくコピーしたクリップボードのテキストを Tkinter エントリに貼り付けます。

新しいテキスト行をコピーすると、GUI が完全に更新されます。

しかし、GUI が応答せず、 をクリックしてTK entry必要なものを入力することができません。

参考までに、私はPython 3.5ソフトウェアを使用しています。前もって感謝します。

私のコード:

from tkinter import *
import pyperclip 

#initial placeholder
#----------------------
old_clipboard = ' '  
new_clipboard = ' '

#The GUI
#--------
root = Tk()

textvar = StringVar()

label1 = Label(root, text='Clipboard')
entry1 = Entry(root, textvariable=textvar)

label1.grid(row=0, sticky=E)
entry1.grid(row=0, column=1)

#while loop
#-----------
while(True): #first while loop: keep monitoring for new clipboard
    while(old_clipboard == new_clipboard): #second while loop: check if old_clipboard is equal to the new_clipboard
        new_clipboard = pyperclip.paste() #get the current clipboard 

    print('\nold clipboard pre copy: ' + old_clipboard)    

    old_clipboard = new_clipboard   #assign new_clipboard to old_clipboard 

    print('current clipboard post copy: ' + old_clipboard)      

    print('\ncontinuing the loop...')

    textvar.set(old_clipboard) #set the current clipboard to GUI entry

    root.update()   #update the GUI
root.mainloop()
4

1 に答える 1

1

while ループを def に入れてから、新しいスレッドで開始する必要があります。そうすれば、GUI はフリーズしません。例えば:

import threading

def clipboardcheck():
    #Your while loop stuff

class clipboardthread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        clipboardcheck()

clipboardthread.daemon=True #Otherwise you will have issues closing your program

clipboardthread().start()
于 2016-04-24T17:04:53.893 に答える