2

I'm kind of a newbie in python. I want to create a program that when you copy something to your clipboard, It 'prints' it on the 'app'. It works, but the issue is that every two seconds it displays what you may have copied 2 hours ago. I want it to be that when the clipboard is the same, it only shows one time and then it waits until you have copied something else. This is what i have so far

import pyperclip
from tkinter import *

r = Tk()
def aper():
    global x
    x = pyperclip.waitForPaste()
    Label(r, text = x).pack()
    r.after(2000, aper)
    
r.after(2000, aper)
r.mainloop()

Thank you!

Luca Janss
  • 81
  • 10
  • You could keep a live copy of whatever the last copy was (you are doing this with x) then check if they match and if they do then do not print to screen. If they don't then print to screen and update x. – Mike - SMT Mar 29 '21 at 14:58

2 Answers2

1

you could make a variable called old_x, store the last value of x in there, and put an if-statement around the Label line, so that it only prints if x is not old_x

import pyperclip
from tkinter import *

r = Tk()
def aper():
    global x
    global old_x
    x = pyperclip.waitForPaste()
    if x != old_x:
        Label(r, text = x).pack()
        old_x = x
    r.after(2000, aper)

old_x = None
r.after(2000, aper)
r.mainloop()
  • 1
    You do not need to use `global` twice. You can do `global x, old_x`. – Mike - SMT Mar 29 '21 at 15:33
  • true, @Mike-SMT, i tend not to use global variables so i went with something i knew would work. I agree there are better ways of handling this but this way is easy to understand and implement, and it works. – aragonnetje6 Mar 29 '21 at 15:36
  • Well it would be easier to use only one global and then replace that global when condition is met. But yes this does work fine. – Mike - SMT Mar 29 '21 at 15:38
  • 1
    it would be, but OP had kept x as a global variable, and i decided not to change that in case he had other reasons for that – aragonnetje6 Mar 29 '21 at 15:40
1

You can simply add an IF statement to check the old value of x and then update when needed.

That said I replace your import of tkinter to be import tkinter as tk. This will help prevent overwriting anything being imported. Just good practice.

Here is a non OOP example.

import tkinter as tk
import pyperclip


r = tk.Tk()
x = ''

def aper():
    global x
    y = pyperclip.waitForPaste()
    if x != y:
        x = y
        tk.Label(r, text=x).pack()
    r.after(2000, aper)


r.after(2000, aper)
r.mainloop()

Here is an OOP example. This helps you prevent the need to use global. In general it is good to avoid global. There are some good post about this if you want to know more.

import tkinter as tk
import pyperclip


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.x = ''
        print('t')

    def aper(self):
        y = pyperclip.waitForPaste()
        if self.x != y:
            self.x = y
            tk.Label(self, text=self.x).pack()

        self.after(2000, self.aper)


if __name__ == '__main__':
    App().mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • No it doesn't work cause you didn't define x? but why did you use: import tkinter as tk instead of from tkinter import *? – Luca Janss Mar 29 '21 at 15:11
  • 2
    By importing * you run the risk of overwriting imported methods. For example if you wrote your own function/method called `Text` it would overwrite the imported `Text` method from tkinter. So by import as something you can be sure that you will never overwrite any of those imported names because you use the `tk.` prefix for everything tkinter imports. – Mike - SMT Mar 29 '21 at 15:15
  • @LucaJanss that was a small oversight. I was just adding the portion that did the work to check. Updated now. – Mike - SMT Mar 29 '21 at 15:22