首页 > 解决方案 > 来自 Automatetheboringstuff 的多个剪贴板项目不起作用

问题描述

我正在通过automatedtheboringstuff 网站学习Python。我为多剪贴板脚本完成了第 8 章项目,但它不起作用。如果我传递“保存”参数,它应该将剪贴板上的任何内容保存到带有关键字的架子文件中,当我传递该关键字时,它应该复制与关键字匹配的任何内容,如果我传递“列表”,它应该复制所有关键字到剪贴板。当我尝试使用任何关键字运行脚本时,什么都没有发生。我在剪贴板上的任何东西都不会改变。

我没有看到我的代码做错了什么,有什么想法吗?

#! python 3
# mcb.pyw - Saves and loads pieces of text to the clipboard.
# Usage: py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.
#       py.exe mcb.pyw <keyword> - Loads keyword to clipboard.
#       py.exe mcb.pyw list - Loads all keywords to clipboard.


import shelve, pyperclip, sys

mcbShelf = shelve.open('mcb')


# Save clipboard content.

if len(sys.argv) == 3 and sys.argv[1].lower() == 'save':
    mcbShelf[sys.argv[2]] = pyperclip.paste()

elif len(sys.argv) == 2:
    # List keywords and load content.

    if sys.argv[1].lower() == 'list':
        pyperclip.copy(str(list(mcbShelf.keys())))

    elif sys.argv[1] in mcbShelf:
        pyperclip.copy(mcbShelf[sys.argv[1]])


mcbShelf.close()

编辑:我有另一个使用 pyperclip 的脚本,批处理文件和脚本存储在完全相同的位置,另一个脚本可以工作,而这个脚本不会更改剪贴板上的任何内容。这是否意味着搁置模块出了问题?

标签: python

解决方案


我有同样的问题。我的操作系统 - Ubuntu,Python 3。

我安装了模块,正如它所指出的那样 - https://pyperclip.readthedocs.io/en/latest/index.html#not-implemented-error。然后它工作了。而不是 pyw 扩展使用 py.

运行代码(在终端 Ubuntu 中): python3 mcb.py

将任何数据从缓冲区保存到文件: python3 mcb.py save anydata

从文件读取任何数据到缓冲区: python3 mcb.py anydata


推荐阅读