如何在python中隐藏控制台窗口?

时间:2022-07-17 00:21:29

I am writing an IRC bot in Python.

我正在用Python编写一个IRC机器人。

I wish to make stand-alone binaries for Linux and Windows of it. And mainly I wish that when the bot initiates, the console window should hide and the user should not be able to see the window.

我希望为Linux和它的Windows制作独立的二进制文件。我希望当机器人启动时,控制台窗口应该隐藏起来,用户不能看到窗口。

What can I do for that?

我能做些什么呢?

6 个解决方案

#1


79  

Simply save it with a .pyw extension. This will prevent the console window from opening.

只需使用.pyw扩展名保存它。这将阻止控制台窗口打开。

On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.

在Windows系统中,没有“可执行模式”的概念。Python安装程序自动将.py文件与Python关联起来。exe,以便在Python文件上双击将其作为脚本运行。扩展名也可以是.pyw,在这种情况下,通常出现的控制台窗口将被抑制。

Explanation at the bottom of section 2.2.2

解释在第2.2.2节的底部

#2


21  

In linux, just run it, no problem. In Windows, you want to use the pythonw executable.

在linux中,只要运行它,没有问题。在Windows中,您希望使用pythonw可执行文件。

Update

Okay, if I understand the question in the comments, you're asking how to make the command window in which you've started the bot from the command line go away afterwards?

好吧,如果我理解评论中的问题,你问的是如何让你在命令行中启动机器人的命令窗口在之后消失?

  • UNIX (Linux)
  • UNIX(Linux)

$ nohup mypythonprog &

美元nohup mypythonprog &

  • Windows
  • 窗户

C:/> start pythonw mypythonprog

C:/ >开始pythonw mypythonprog

I think that's right. In any case, now you can close the terminal.

我认为这是对的。无论如何,现在您可以关闭终端。

#3


16  

On Unix Systems (including Linux, macOS, and BSD)

Use nohup mypythonprog &, and you can close the terminal window without disrupting the process. You can also run exit if you are running in the cloud and don't want to leave a hanging shell process.

使用nohup myonprog &,您可以关闭终端窗口,而不会中断进程。如果您在云中运行,并且不想留下挂起的shell进程,您也可以运行exit。

On Windows Systems

Save the program with a .pyw extension and now it will open with pythonw.exe. No shell window.

使用.pyw扩展名保存程序,现在将使用python .exe打开程序。没有shell窗口。

For example, if you have foo.py, you need to rename it to foo.pyw.

例如,如果你有foo。py,您需要将它重命名为foo.pyw。

#4


6  

If all you want to do is run your Python Script on a windows computer that has the Python Interpreter installed, converting the extension of your saved script from '.py' to '.pyw' should do the trick.

如果您只想在安装了Python解释器的windows计算机上运行您的Python脚本,那么将保存的脚本的扩展从' '转换为'。py’”。pyw'应该可以做到。

But if you're using py2exe to convert your script into a standalone application that would run on any windows machine, you will need to make the following changes to your 'setup.py' file.

但是,如果您正在使用py2exe将您的脚本转换为一个可以在任何windows机器上运行的独立应用程序,您将需要对“安装”进行以下更改。py文件。

The following example is of a simple python-GUI made using Tkinter:

下面的例子是使用Tkinter制作的一个简单的python-GUI:

from distutils.core import setup
import py2exe
setup (console = ['tkinter_example.pyw'],
       options = { 'py2exe' : {'packages':['Tkinter']}})

Change "console" in the code above to "windows"..

将上面代码中的“控制台”更改为“windows”。

from distutils.core import setup
import py2exe
setup (windows = ['tkinter_example.pyw'],
       options = { 'py2exe' : {'packages':['Tkinter']}})

This will only open the Tkinter generated GUI and no console window.

这将只打开Tkinter生成的GUI,没有控制台窗口。

#5


2  

Some additional info. for situations that'll need the win32gui solution posted by Mohsen Haddadi earlier in this thread:

一些额外的信息。对于需要Mohsen Haddadi在本文前面发布的win32gui解决方案的情况:

As of python 361, win32gui & win32con are not part of the python std library. To use them, pywin32 package will need to be installed; now possible via pip.

对于python 361, win32gui和win32con不是python std库的一部分。要使用它们,需要安装pywin32包;现在可能通过皮普。

More background info on pywin32 package is at: How to use the win32gui module with Python?.

关于pywin32包的更多背景信息是:如何在Python中使用win32gui模块?

Also, to apply discretion while closing a window so as to not inadvertently close any window in the foreground, the resolution could be extended along the lines of the following:

此外,为了在关闭窗口时应用谨慎性,以避免无意中关闭前景中的任何窗口,决议可以沿着以下几行进行扩展:

try     :

    import win32gui, win32con;

    frgrnd_wndw = win32gui.GetForegroundWindow();
    wndw_title  = win32gui.GetWindowText(frgrnd_wndw);
    if wndw_title.endswith("python.exe"):
        win32gui.ShowWindow(frgrnd_wndw, win32con.SW_HIDE);
    #endif
except  :
    pass

#6


2  

This will hide your console. Implement this lines in your code first to start hiding your console at first.

这将隐藏您的控制台。首先在代码中实现这一行,首先开始隐藏控制台。

import win32gui, win32con

The_program_to_hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(The_program_to_hide , win32con.SW_HIDE)

#1


79  

Simply save it with a .pyw extension. This will prevent the console window from opening.

只需使用.pyw扩展名保存它。这将阻止控制台窗口打开。

On Windows systems, there is no notion of an “executable mode”. The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.

在Windows系统中,没有“可执行模式”的概念。Python安装程序自动将.py文件与Python关联起来。exe,以便在Python文件上双击将其作为脚本运行。扩展名也可以是.pyw,在这种情况下,通常出现的控制台窗口将被抑制。

Explanation at the bottom of section 2.2.2

解释在第2.2.2节的底部

#2


21  

In linux, just run it, no problem. In Windows, you want to use the pythonw executable.

在linux中,只要运行它,没有问题。在Windows中,您希望使用pythonw可执行文件。

Update

Okay, if I understand the question in the comments, you're asking how to make the command window in which you've started the bot from the command line go away afterwards?

好吧,如果我理解评论中的问题,你问的是如何让你在命令行中启动机器人的命令窗口在之后消失?

  • UNIX (Linux)
  • UNIX(Linux)

$ nohup mypythonprog &

美元nohup mypythonprog &

  • Windows
  • 窗户

C:/> start pythonw mypythonprog

C:/ >开始pythonw mypythonprog

I think that's right. In any case, now you can close the terminal.

我认为这是对的。无论如何,现在您可以关闭终端。

#3


16  

On Unix Systems (including Linux, macOS, and BSD)

Use nohup mypythonprog &, and you can close the terminal window without disrupting the process. You can also run exit if you are running in the cloud and don't want to leave a hanging shell process.

使用nohup myonprog &,您可以关闭终端窗口,而不会中断进程。如果您在云中运行,并且不想留下挂起的shell进程,您也可以运行exit。

On Windows Systems

Save the program with a .pyw extension and now it will open with pythonw.exe. No shell window.

使用.pyw扩展名保存程序,现在将使用python .exe打开程序。没有shell窗口。

For example, if you have foo.py, you need to rename it to foo.pyw.

例如,如果你有foo。py,您需要将它重命名为foo.pyw。

#4


6  

If all you want to do is run your Python Script on a windows computer that has the Python Interpreter installed, converting the extension of your saved script from '.py' to '.pyw' should do the trick.

如果您只想在安装了Python解释器的windows计算机上运行您的Python脚本,那么将保存的脚本的扩展从' '转换为'。py’”。pyw'应该可以做到。

But if you're using py2exe to convert your script into a standalone application that would run on any windows machine, you will need to make the following changes to your 'setup.py' file.

但是,如果您正在使用py2exe将您的脚本转换为一个可以在任何windows机器上运行的独立应用程序,您将需要对“安装”进行以下更改。py文件。

The following example is of a simple python-GUI made using Tkinter:

下面的例子是使用Tkinter制作的一个简单的python-GUI:

from distutils.core import setup
import py2exe
setup (console = ['tkinter_example.pyw'],
       options = { 'py2exe' : {'packages':['Tkinter']}})

Change "console" in the code above to "windows"..

将上面代码中的“控制台”更改为“windows”。

from distutils.core import setup
import py2exe
setup (windows = ['tkinter_example.pyw'],
       options = { 'py2exe' : {'packages':['Tkinter']}})

This will only open the Tkinter generated GUI and no console window.

这将只打开Tkinter生成的GUI,没有控制台窗口。

#5


2  

Some additional info. for situations that'll need the win32gui solution posted by Mohsen Haddadi earlier in this thread:

一些额外的信息。对于需要Mohsen Haddadi在本文前面发布的win32gui解决方案的情况:

As of python 361, win32gui & win32con are not part of the python std library. To use them, pywin32 package will need to be installed; now possible via pip.

对于python 361, win32gui和win32con不是python std库的一部分。要使用它们,需要安装pywin32包;现在可能通过皮普。

More background info on pywin32 package is at: How to use the win32gui module with Python?.

关于pywin32包的更多背景信息是:如何在Python中使用win32gui模块?

Also, to apply discretion while closing a window so as to not inadvertently close any window in the foreground, the resolution could be extended along the lines of the following:

此外,为了在关闭窗口时应用谨慎性,以避免无意中关闭前景中的任何窗口,决议可以沿着以下几行进行扩展:

try     :

    import win32gui, win32con;

    frgrnd_wndw = win32gui.GetForegroundWindow();
    wndw_title  = win32gui.GetWindowText(frgrnd_wndw);
    if wndw_title.endswith("python.exe"):
        win32gui.ShowWindow(frgrnd_wndw, win32con.SW_HIDE);
    #endif
except  :
    pass

#6


2  

This will hide your console. Implement this lines in your code first to start hiding your console at first.

这将隐藏您的控制台。首先在代码中实现这一行,首先开始隐藏控制台。

import win32gui, win32con

The_program_to_hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(The_program_to_hide , win32con.SW_HIDE)