![](/img/trans.png)
[英]Program throws (Non-UTF-8 code starting with '\xff' in file) when running from command prompt
[英]Pygame program looking different when debuging from visual studio code versus running from command prompt
我正在使用 pygame 在 python 中下棋。 開始菜單由兩個按鈕組成。 它們的邊框使用
pygame.draw.rect(screen, (255, 255, 255), text1rect.inflate(20, 20), 10)
這是我用來執行此操作的代碼:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Chess")
icon = pygame.image.load("./Images/wking.png").convert_alpha()
pygame.display.set_icon(icon)
font = pygame.font.Font("Roboto-Regular.ttf", 32)
text1 = font.render("Create game", True, (0, 255, 0), (124, 124, 124))
text1_rect = text1.get_rect(center = (400, 300))
text2 = font.render("Join game", True, (0, 255, 0), (124, 124, 124))
text2_rect = text2.get_rect(center = (400, 500))
board = pygame.image.load("./Images/board.png").convert()
def main():
scene_to_render = "menu"
clock = pygame.time.Clock()
while True:
mouse_pos = pygame.mouse.get_pos()
is_clicked = pygame.mouse.get_pressed()[0]
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if scene_to_render == "menu":
screen.fill((0, 0, 0))
screen.blit(text1, text1_rect)
screen.blit(text2, text2_rect)
pygame.draw.rect(screen, (255, 255, 255), text1_rect.inflate(20, 20), 10)
pygame.draw.rect(screen, (255, 255, 255), text2_rect.inflate(20, 20), 10)
if text1_rect.collidepoint(mouse_pos) and is_clicked:
pass
if text2_rect.collidepoint(mouse_pos) and is_clicked:
pass
# Obviously there is more code but that is not important because only the menu buttons are broken when starting from the command prompt
pygame.display.update()
clock.tick(16)
if __name__ == "__main__":
main()
從命令提示符啟動時,窗口和任務欄圖標也不會出現,被標准的 python 應用程序圖標替換。 我知道在使用命令使用 pyinstaller 編譯為 .exe 時可以解決此問題。
用戶 Zack 在以下答案中解釋了這一點:
https://stackoverflow.com/a/10438300/18072034
以 pyinstaller 為例,您可以調用 python pyinstaller.py --icon=icon.ico
這是從 vscode 啟動時菜單的外觀:
這是從 cmd 運行時菜單的外觀:
我實際上在寫問題時就想到了這一點,在將 pip 和 pygame 升級到最新版本后,通過在命令提示符中輸入以下命令來解決問題:(確保在執行此操作時以管理員身份打開命令提示符)
pip install --upgrade pip
和
pip install --upgrade pygame
另外,因為我在完成問題之前解決了問題,所以我無法對問題進行截圖。 顯示損壞菜單的圖像經過編輯,但與實際外觀非常相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.