如何在全屏应用程序的后台运行 python 脚本

How to run a python script in the background of a fullscreen application

提问人:Rexdestoyer 提问时间:8/25/2023 更新时间:8/25/2023 访问量:52

问:

我正在尝试运行一个 python 脚本,该脚本将在全屏游戏中在后台移动文件。当程序运行时,我希望它不会将我排除在游戏之外。我在 Windows 10 上。

我可以通过鼠标上的按钮运行程序,但是当它运行时,命令提示符会短暂打开,游戏会失去焦点。

python windows-10 后台进程 失去焦点

评论

0赞 Musabbir Arrafi 8/25/2023
我认为没有其他方法可以解决它:3
0赞 MSH 8/25/2023
创建新的线程或进程可能会有所帮助。

答:

0赞 Ahnaf Tahmid Zaman 8/25/2023 #1

在 cmd 上键入以下命令:

start /b python your_script.py

/bswitch 告诉 cmd 在后台启动脚本。

评论

0赞 Rexdestoyer 8/25/2023
我尝试使用 .bat 文件运行您的代码,但它仍然专注于命令提示符我运行的代码:@ECHO OFF ECHO Attempting to run file in background cd (path) start /b python FileMover.py
0赞 Rexdestoyer 8/25/2023
有没有办法让程序重新聚焦游戏?
0赞 Rexdestoyer 8/25/2023 #2

在看到它可能是不可能的或需要线程后,我放弃了可能在后台打开它。取而代之的是,当游戏完成运行时,我让代码专注于游戏。

def find_window(title):
    return win32gui.FindWindow(None, title)

def bring_to_foreground(hwnd):
    win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)  # Restore the window if minimized
    win32gui.SetForegroundWindow(hwnd)

app_title = "APPLICATION_NAME"
app_window = find_window(app_title)

if app_window:
    bring_to_foreground(app_window)
    print(f"Window '{app_title}' brought to the foreground.")
else:
    print(f"Window '{app_title}' not found.")

将应用程序名称替换为所需应用程序的名称。