tkinter 和 pygame 不想在一个窗口中工作 [英] tkinter and pygame do not want to work in one window
问题描述
我想创建一个应用程序,并希望将 tkinter 用作 GUI,并将游戏库之一用作 Pyglet 和 Pygame.我没有找到任何关于将 pyglet 嵌入 tkinter 的信息,但找到了一些带有 tkinter 和 pygame 的代码:将 Pygame 窗口嵌入到 Tkinter 或 WxPython 框架中.我正在使用 python 3.7,这是我使用的代码:
I want to create an application and want to use tkinter as GUI and one of game libraries as Pyglet and Pygame. I didn't find any info about embedding pyglet into tkinter but found some code with tkinter and pygame: Embedding a Pygame window into a Tkinter or WxPython frame. I'm using python 3.7 and here is the code that I use:
import pygame
import tkinter as tk
from tkinter import *
import os
root = tk.Tk()
embed = tk.Frame(root, width=500, height=500)
embed.grid(columnspan=600, rowspan=500)
embed.pack(side=LEFT)
buttonwin = tk.Frame(root, width=75, height=500)
buttonwin.pack(side=LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500, 500))
screen.fill(pygame.Color(0, 255, 255))
pygame.display.init()
pygame.display.update()
def draw():
pygame.draw.circle(screen, (0, 0, 0), (250, 250), 125)
pygame.display.update()
button1 = Button(buttonwin, text='Draw', command=draw)
button1.pack(side=LEFT)
root.update()
while True:
pygame.display.update()
root.update()
当我只想创建一个窗口时,它会创建 2 个窗口.出了什么问题?谢谢!
It creates 2 windows when I want to create only one. What is going wrong? Thanks!
推荐答案
感谢 acw1668!在这里你可以下载1.9.6 pygame轮子:https://pypi.org/project/pygame/1.9.6/#files.在 python 3.8 上一切正常!
Thanks to acw1668! Here you can download 1.9.6 pygame wheels: https://pypi.org/project/pygame/1.9.6/#files. And everything works fine with python 3.8!
这篇关于tkinter 和 pygame 不想在一个窗口中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!