在 Windows 上执行脚本时隐藏控制台 [英] Hiding the console while executing a script on Windows

查看:64
本文介绍了在 Windows 上执行脚本时隐藏控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在执行脚本时隐藏我的控制台?我想创建一个单独的函数来做到这一点(也许通过应用 os/sys 中的一些方法,我不知道),所以我不需要像将脚本扩展名从 .py 更改为 .pyw.

How can I make my console hide while executing a script? I want to make a separate function to do it (maybe by applying some methods from os/sys, I don't know), so I do not need some solutions like changing script extension from .py to .pyw.

推荐答案

我在 Windows 7 上的做法是制作一个快捷方式(或链接),使用 pythonw.exe 运行我的脚本code> 解释器,它没有控制台,而不是默认的 python.exe.

The way I've done it on Windows 7 is by making a shortcut (or link) that runs my script with the pythonw.exe interpreter, which has no console, instead of the default python.exe.

只需遵循以下 3 个步骤:

Just follow these 3 steps:

  1. 首先为您的脚本创建一个普通的快捷方式.一种方法是将资源管理器窗口中显示的脚本文件的图标拖到您希望快捷方式所在的位置(如桌面),并在释放鼠标按钮时按住 Alt 键.
  2. 右键单击刚刚创建的快捷方式,然后从弹出的菜单中选择Properties.
  3. 将出现快捷方式的属性对话框.在它的脚本文件路径前插入 C:\python27\pythonw.exe 和一个空格.如果脚本的路径中有任何空格,现在必须用双引号括起来._如果您使用的是其他版本的 Python,您还将需要相应地更改 Python27.
  1. First create a normal shortcut to your script. One way to do this is to drag the icon for the script file shown in an Explorer window to where you want the shortcut to be (like the desktop) and hold down the Alt key when releasing the mouse button.
  2. Right-click on the just created shortcut and select Properties from the the menu that pops-up.
  3. A Properties dialog for the shortcut will appear. In it insert C:\python27\pythonw.exe and a space before the path to the your script file. if the path to your script has any spaces in it, it must now be enclosed in double quotes._ If you're using another version of Python, you'll also need to change the Python27 accordingly.

D:\path 中包含空格到\myscript.py 的目标需要更改

C:\Python27\pythonw.exeD:\path 中带有空格到\myscript.py"

如果您愿意,您也可以在此处更改快捷方式的图标.

You can also change the shortcut's icon here if you wish.

这是一个例子:

更新 - 更简单的方法:您还可以将脚本的扩展名更改为 .pyw,这将使其与 pythonw.exe<一起运行/code> 而不是 python.exe.

Update - simpler way: You can also change the extension of your script to .pyw which will cause it to be run with the pythonw.exe instead of the python.exe.

这篇关于在 Windows 上执行脚本时隐藏控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆