2016-04-24 21 views
2

私はプログラミングの初心者です。私は趣味としてこれを行い、職場での生産性を向上させます。whileループでupdate()を使用するとTkinterウィンドウが応答しない

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

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

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

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

FYI私は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() 

答えて

0

あなたはそのように、あなたのGUI won't凍結、その後、新しいスレッドでそれを開始し、DEFであなたのwhileループを配置する必要があります。 例:

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() 
関連する問題