Python (TKinter + Turtle) – «не удалось удалить временный каталог»Python

Программы на Python
Anonymous
Python (TKinter + Turtle) – «не удалось удалить временный каталог»

Сообщение Anonymous »

Я создаю приложение Python, используя pygame, черепаху и tkinter. Хотя в моей IDE (PyCharm) все работает нормально, я столкнулся с ошибкой при попытке перезапустить программу с помощью события onkey из tkinter в файле .exe. Ошибка:
.exe создается с помощью Pyinstaller: pyinstaller --onefile --windowed --icon=axe.ico main.py
Не удалось удалить временный каталог: C:/users/lucky/AppData/Local/Temp/_MEI135042
Вот мой код:
import os
import sys
import tkinter
import turtle
import json
import tempfile
import shutil

os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "True"
import pygame

STANDARD_KEY_BINDING = ["a", "d", "q"]

def cleanup():
try:
turtle.bye()
except turtle.Terminator:
pass

pygame.quit()

temp_dir = tempfile.gettempdir()
try:
for temp_item in os.listdir(temp_dir):
temp_path = os.path.join(temp_dir, temp_item)
if os.path.isdir(temp_path) and "your-app-name" in temp_item:
shutil.rmtree(temp_path)
print(f"Removed temporary directory: {temp_path}")
except Exception as e:
print(f"Error removing temporary directory: {e}")

def restart_program():
cleanup()
python = sys.executable
os.execl(python, python, *sys.argv)

def game(map_choice):
# [non-related code]

background.screen.onkey(fun=player.move_left_swing, key=binds[0])
background.screen.onkey(fun=player.move_right_swing, key=binds[1])
background.screen.onkey(fun=restart_program, key=binds[2])

# [non-related code]

When the function from background.screen.onkey(fun=restart_program, key=binds[2]) is called, There is an "failed to remove temporary directory" error. The application should return to the main window created using tkinter.

What I've Tried:
Verified that the temporary directories are accessible and ensured that pygame.quit() and turtle.bye() are called in the cleanup() function.

Any kind of help on how to fix this error would be appreciated!


Подробнее здесь: https://stackoverflow.com/questions/790 ... -directory

Вернуться в «Python»