我试图在Python上打印剪贴板内容,但是我的代码不起作用

时间:2020-05-17 05:15:20

标签: python clipboard

我是python的新手(并且通常是编码),在学习了基本语法和命令后,我尝试制作一个脚本,打算复制剪贴板内容,然后将其打印出来的脚本。

这是代码:

#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")

我做错了什么?语法不好吗?当我执行.py文件时,控制台不会显示任何内容。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

首先是晚安(如果您现在居住的地方是晚上,则应做好其他准备) 所以,

pasteText函数中,您忘记了放置return 因此该函数在执行时会给出该值作为响应 tsc,tsc,tsc 像这样:

def pasteTest():
    return clipboard.paste()

,然后在howLong的{​​{1}}函数中,我想您要检查剪贴板是否有任何长度,但是最终将函数与数字进行比较 同样,检查给定字符串是否具有任何长度的一种好方法是这样的:

if (howLong>1):

因为没有长度,该语句会将其视为False,因此代码为:

if given_string:
    something()

但是我们是程序员,程序员想要尽可能高的效率,并认为我们可以将if放在一行中,并且只使用一张印刷品, howww ???? 像这样:

def howLong():

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

语法def howLong(): print(pasteTest() or "No content on the clipboard") 检查a or b是否包含任何内容,如果是,则考虑a,否则(当a == None时)考虑b


但是,如果那是您的整个程序文件,我会说您忘记了调用howLong函数,这可能就是为什么它不起作用的原因,所以只需添加:

a

最后