简体   繁体   English

使用pyw文件的Windows启动不会关闭cmd

[英]Windows startup with pyw file doesn't close cmd

I've been working on a small script that's supposed to run in the background of my computer.我一直在研究一个应该在我的电脑后台运行的小脚本。 I already have the script working and everything except one thing that hours of google searching and hunting have not found the answer to.我已经让脚本正常工作,除了谷歌搜索和狩猎数小时还没有找到答案的一件事。

The file has a.pyw extension, and when I run it from the command prompt:该文件的扩展名为 .pyw,当我从命令提示符运行它时:

pythonw File.pyw

the cmd window will continue on and give me another prompt without launching idle like it would with a regular.py file. cmd 窗口将继续并给我另一个提示,而不会像使用 regular.py 文件那样启动空闲。

When I double-click the file, the cmd window opens and closes without a problem, just like it's supposed to.当我双击该文件时,cmd 窗口打开和关闭没有问题,就像它应该的那样。 This is all perfect for me.这对我来说是完美的。

However, I tried to make a small batch file:但是,我试图制作一个小的批处理文件:

cd C:\Users\(my name)\Desktop
pythonw File.pyw

and I stuck that in the startup folder of windows.我把它贴在 Windows 的启动文件夹中。 However, when I restarted my computer to see if it would work, it opened the cmd window, but wouldn't close it.但是,当我重新启动计算机以查看它是否可以工作时,它打开了 cmd 窗口,但不会关闭它。 I can't figure out why.我不知道为什么。 I've tried everything I can think of, including sticking the File.pyw directly in the startup folder, and trying to put an exit command right in the batch file like so:我已经尝试了所有我能想到的方法,包括将 File.pyw 直接粘贴到启动文件夹中,并尝试将退出命令放在批处理文件中,如下所示:

cd C:\Users\(my name)\Desktop
pythonw File.pyw
exit

But, as you can probably guess, that failed.但是,正如您可能猜到的那样,那失败了。 I tried putting the command directly in my code, so right before the end, it had the line我试着把命令直接放在我的代码中,所以就在结束之前,它有一行

os.system("exit")

but after realizing this wouldn't work, I just took it out.但在意识到这行不通之后,我就把它拿出来了。 (Important detail: the last line of the code is set to loop it until the program closes. That's why I'm trying to use the pyw extension, so the console can close before the file ends) (重要细节:代码的最后一行设置为循环直到程序关闭。这就是我尝试使用 pyw 扩展名的原因,因此控制台可以在文件结束之前关闭)

Next, I shortened the batch file to only be the one line:接下来,我将批处理文件缩短为只有一行:

pythonw.exe C:\Users\(my name)\Desktop\File.pyw

but it still won't work.但它仍然行不通。 When the batch file is run, I get an open cmd window with the command entered, but it runs like a regular.py file, not closing the cmd window.运行批处理文件时,我会打开一个输入命令的 cmd 窗口,但它像 regular.py 文件一样运行,而不是关闭 cmd 窗口。

Can anyone help me figure out why the console won't close when the command is run from a.batch file, but will when run directly from command prompt?谁能帮我弄清楚为什么当从 a.batch 文件运行命令时控制台不会关闭,但是当直接从命令提示符运行时会关闭?

UPDATE :更新

the script is meant to add a quick keyboard shortcut to close a task, specifically Google Chrome, when I press '+' twice quickly.该脚本旨在添加一个快速键盘快捷键以在我快速按两次“+”时关闭任务,特别是 Google Chrome。 Here's a my full code (minus some personal info)这是我的完整代码(减去一些个人信息)

import os
import sys
import pyHook, pythoncom

setting key to be '+' to avoid accidental usage.
def OnKeyBoardEvent(event):
    global prevPlus, escPushed
    if event.Ascii == 43:
        if prevPlus == None:
            prevPlus = event.Time
        else:
            if event.Time - prevPlus <= 1000:
                os.system("taskkill /IM chrome.exe")
                prevPlus = event.Time
            else:
                prevPlus = event.Time
    elif event.Ascii == 27:
        if escPushed == None:
            escPushed = event.Time
        else:
            if event.Time - escPushed <= 1000:
            sys.exit()
            else:
                escPushed = event.Time


getter = pyHook.HookManager()
getter.KeyDown = OnKeyBoardEvent
getter.HookKeyboard()

prevPlus = None
escPushed = None

pythoncom.PumpMessages()

This all works perfectly when I'm running it from pycharm, or from cmd, but when I put it in C:\Users(my name)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup and then try it out, nothing happens.当我从 pycharm 或从 cmd 运行它时,这一切都完美无缺,但是当我把它放在 C:\Users(my name)\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 然后尝试它时出去,什么也没有发生。 I'm guessing it's because windows runs the file and exits it before anything happens, but I'm really not sure.我猜这是因为 Windows 运行文件并在任何事情发生之前退出它,但我真的不确定。 I just really need a solution that makes it run at startup and take my key input until I shut down.我真的需要一个解决方案,让它在启动时运行并接受我的按键输入,直到我关闭。

Use windows 'start' command in your.cmd file;在您的 .cmd 文件中使用 Windows“启动”命令; remove the 'exit'.删除“退出”。
Like this:像这样:

cd C:\Users\(my name)\Desktop
start pythonw File.pyw

So, my final approach was just to link a VBS file (like it was mentioned in the comments.) It turned out I was doing it wrong, and mixing file types, so I had weird errors.所以,我最后的方法只是链接一个 VBS 文件(就像评论中提到的那样。)结果证明我做错了,并且混合了文件类型,所以我出现了奇怪的错误。 That VBS file called a BAT file silently, so the cmd window didn't show up.那个 VBS 文件默默地调用了一个 BAT 文件,所以 cmd 窗口没有出现。 That BAT file then called my program, which for some reason ran better when called from cmd than when it was just executed on the spot.然后那个 BAT 文件调用了我的程序,由于某种原因,从 cmd 调用时比当场执行时运行得更好。 Now, it does run on startup, and the cmd window doesn't appear, so it is a good fix, though inefficient.现在,它确实在启动时运行,并且没有出现 cmd 窗口,所以这是一个很好的修复,虽然效率低下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM