2

I am a beginner in programming. I do this as hobby and to improve my productivity at work.

I am writing a program to automatically paste the clipboard to a Tkinter entry whenever a user copy a line of text.

I use a while loop to detect if there is a change in the current clipboard, then paste the newly copied clipboard text to the Tkinter entry.

The GUI update perfectly when I copy a new line of text.

However the GUI is not responding and I can't click the TK entry to type something that I want.

FYI I am using Python 3.5 software. Thanks in advance.

My code:

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()
lanette
  • 35
  • 10

1 Answers1

1

You need to put your while loop in a def, then start it in a new thread, that way your gui won´t freeze. eg:

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