Search code examples
pythonclipboard

I tried to print clipboard content on Python but my code doesn't work


I'm new on python (and coding in general) and after learning the basic syntax and commands I tried to make a script that intends to copy the clipboard content and then print it as a result of the script.

Here's the code:

#clipboard is the module that I found on pyp that manages clipboard content.

import clipboard 

#This module only has two commands, I'm using the second in my code:

#clipboard.copy("abc")  - The clipboard content will be string "abc"
#text = clipboard.paste()  - text will have the content of clipboard

def pasteTest():
    clipboard.paste()


def howLong():
    len(pasteTest)

    if (howLong>1):
            print(pasteTest())

    else:
            print("No content on the clipboard")

What I'm doing wrong? Is the syntax bad? When I execute the .py file the console don't show me anything.

I'd appreciate your help.


Solution

  • Firstly good night (if where you live is now at night, otherwise good something) so,

    at the pasteText function you have forgotten to put the return so the function when executed gives that value as response tsc, tsc, tsc like this:

    def pasteTest():
        return clipboard.paste()
    

    and in the howLong function at if (howLong>1): I think you wanted to check if the clipboard has any length, but it ended up comparing a function to a number also, a good way to check if a given string has any length is like this:

    if given_string:
        something()
    

    because if it has no length, the statement will consider it as being False, so the code would be:

    def howLong():
    
       if (pasteTest()):
               print(pasteTest())
       else:
               print("No content on the clipboard")
    

    but we are programmers and programmers want efficiency as high as possible, and thinking on that we can make the if in one line and use only one print, howww???? like this:

    def howLong():
    
        print(pasteTest() or "No content on the clipboard")
    

    the syntax a or b checks if a has any content, if yes, a is considered, else (when a == None) b is considered


    but if that is your whole program file, I will say that you forgot to call your howLong function, and that might be why it wasn't working so just add:

    howLong()
    

    at the end