运行PyInstaller生成的可执行文件时下载和替换文件的问题以及其他问题

问题描述 投票:0回答:1

我在尝试使用 PyInstaller 下载和替换 Python 应用程序中的文件时遇到问题。我的应用程序包括从互联网下载文件并替换本地现有文件。 (更新我的游戏文件)。

当我直接运行Python脚本(.py)时,代码工作得很好,但是当我使用PyInstaller(pyinstaller --onefile --noconsole --icon=icone.ico PWULauncher.py)生成可执行文件时,下载的文件以 0 字节的临时格式保存(例如 .1ad61sadtemp)并且未正确替换。

此外,负责启动游戏的功能在运行可执行文件时也不起作用,同样的问题。

我尝试了多种解决方案,例如以管理员身份运行可执行文件、设置文件夹权限以及在 PyInstaller 中使用不同的标志,但问题仍然存在。

这是负责下载和替换文件的代码片段:

# Code snippet responsible for downloading and replacing files
def DownloadFile(self, url, destination):
    try:
        gdown.download(url, destination, quiet=False)
        print("File downloaded successfully:", destination)
    except Exception as e:
        print("Error downloading the file:", e)

# Calling the function to download a file
self.DownloadFile("FILE_URL", "LOCAL_DESTINATION")

这是启动我的游戏的代码:

# Code snippet responsible for starting the game
def start_game(self, game_exe_path, username, password, character_name, idx):
    script_dir = os.path.dirname(os.path.abspath(__file__))  # Get the script directory
    current_dir = os.getcwd()  # Store the current directory
    os.chdir(script_dir)  # Change to the script directory
    game_dir = os.path.join(script_dir, "game")  # Define the game directory
    os.chdir(game_dir)  # Change to the game directory
    try:
        if os.path.exists(game_exe_path):
            subprocess.Popen([game_exe_path, 'startbypatcher', f'user:{username}', f'pwd:{password}', f'role:{character_name}'], creationflags=subprocess.CREATE_NO_WINDOW)
            # Add the new game window to the list
            self.game_windows.append(game_exe_path)
            # Add hotkey for the new window
            hotkey = f'ctrl+shift+{idx}'
            self.hotkeys.append(hotkey)
            try:
                keyboard.add_hotkey(hotkey, lambda idx=len(self.game_windows) - 1: self.focus_window(idx))
                print(f"Hotkey '{hotkey}' added successfully for the window {len(self.game_windows)}")
            except Exception as e:
                print(f"Error adding hotkey '{hotkey}':", e)
        else:
            messagebox.showerror("Error", "The game executable was not found.")
    finally:
        os.chdir(script_dir)  # Restore the original directory

是否有人对以可执行文件形式运行应用程序时可能导致这些问题的原因有任何见解或建议? (也以 .pyw 形式出现)

任何帮助或指导将不胜感激!

如果有帮助,我的进口:

import tkinter as tk
from tkinter import messagebox, ttk
import os
import threading
import gdown
import json
import subprocess
import time
import pygetwindow as gw
import keyboard
import win32gui
import win32con

我在使用 PyInstaller 的 Python 应用程序中遇到问题,当我直接运行 Python 脚本 (.py) 时,代码工作得很好。

我尝试了多种解决方案,例如以管理员身份运行可执行文件、设置文件夹权限以及在 PyInstaller 中使用不同的标志,但问题仍然存在。

python python-3.x pyinstaller
1个回答
0
投票

进行进一步测试后,我成功解决了开始游戏按钮的问题。不过,更新功能仍然存在问题。

我发现只有当我在启用控制台且没有“--noconsole”的情况下生成.exe时,它才有效。 pyinstaller --onefile --icon=icone.ico PWULauncher.py

有谁知道如何禁用控制台而不导致此更新功能出现问题?

© www.soinside.com 2019 - 2024. All rights reserved.