首页 > 解决方案 > pyinstaller 无法执行脚本,这是一个与 idle 一起使用的简单脚本

问题描述

我做了一个小工具来将 ctrl+win+right 箭头绑定到此代码中的一个键,它的 F2 问题是当我尝试使用 pyinstaller 使其成为我想要这个工具的朋友的可执行文件时,当我尝试打开exe它说无法执行脚本。但它与python idle一起正常工作,代码是:

from pynput.keyboard import Key , Controller

keyboard = Controller()

i=2

import keyboard
import time
def waitUntil(): #defines function
    wU = True
    while wU == True:
        if not keyboard.is_pressed("F2"): #checks the condition
            wU = False
        else:
            wU = True

while i == 2 :
    if keyboard.is_pressed("F2") :
        waitUntil()
        keyboard.press("Ctrl+cmd+Right")
        keyboard.release("Ctrl+cmd+Right")```

标签: pythonpyinstallerpy2exe

解决方案


在这里很难提供帮助,因为最终 Controller 没有名为“is_pressed()”的方法。

像这样清理你的代码:

from pynput.keyboard import Controller

keyboard = Controller()

def waitUntil(): #defines function
    wU = True
    while wU == True:
        if not keyboard.is_pressed("F2"): #checks the condition
            wU = False
        else:
            wU = True

while True :
    if keyboard.is_pressed("F2") :
        waitUntil()
        keyboard.press("Ctrl+cmd+Right")
        keyboard.release("Ctrl+cmd+Right")

运行时应该会出现以下错误:

Traceback (most recent call last):
  File "/home/oubnouquestion.py", line 14, in <module>
    if keyboard.is_pressed("F2") :
AttributeError: 'Controller' object has no attribute 'is_pressed'

至少这是我在 Linux 上使用最新版本的 pynput 得到的。因此,它甚至在我使用 Pyinstaller 之前就坏了。你确定这在空闲时有效吗?


推荐阅读