Python:Pygame安装但运行不佳,如果有的话。(麦克,莫哈韦)



我一直在尝试让pygame在tkinter中工作,但我得到的只是两个单独的窗口,其他人都说它可以工作:https://stackoverflow.com/a/16550818/12221209

但是当我运行基本的pygame代码时,我得到的只是码头上一个弹跳的宇宙飞船图标 - 与终端中的外星人演示相同。任何人都知道如何解决这个问题,因为它让我发疯。我想要的只是一个 tkinter 窗口中的 pygame 窗口。

import pygame
import tkinter as tkinter
from tkinter import *
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

我想要的只是 tkinter 窗口中的 pygame 窗口。

我已经采用了您引用的代码并对其进行了清理,以更好地适应Python 3和PEP8。

在您用一个显示您所描述的问题的示例更新您的问题之前,这是我能为您的问题做的最好的事情。

如果您有任何问题,请告诉我。

import tkinter as tk
import pygame
import os

def pygame_update_loop():
pygame.display.update()
# Use an after loop to update the pygame window.
# set the time in milliseconds as desired.
root.after(100, pygame_update_loop)

def draw():
pygame.draw.circle(screen, (0, 0, 0), (250, 250), 125)

root = tk.Tk()
pygame_frame = tk.Frame(root, width=500, height=500)
pygame_frame.pack(side='left')
tk.Button(root, text='Draw',  command=draw).pack(side='left')
os.environ['SDL_WINDOWID'] = str(pygame_frame.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
width, height = 300, 200
screen = pygame.display.set_mode((width, height))
screen.fill(pygame.Color(255, 255, 255))
pygame.display.init()
pygame_update_loop()
root.mainloop()

最新更新