1

I've been trying to get pygame to work in tkinter, but all i get is two separate windows where everyone else says it works: https://stackoverflow.com/a/16550818/12221209

But when I run basic pygame code and all I get is a bouncing spaceship icon on the dock- the same with the aliens demo in terminal. Anybody know how to fix this, because it's driving me insane. All I want is a pygame window inside a tkinter window.

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
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
rk1007
  • 11
  • 1
  • 1st problem is you are importing tkinter twice. Delete `from tkinter *`. Btw `import tkinter as tkinter` is exactly the same as `import tkinter`. So instead for shorter use of tkinter you can `import tkinter as tk` so all your prefix use can simply be `tk.`. – Mike - SMT Oct 15 '19 at 16:51
  • After reviewing the post you referenced I would like to say there are many things wrong with that post. There are at least 5 main issues and some smaller ones. You may want to follow a different example. That said your code example does not even use tkinter in any way. Do you have a testable example we can look at? – Mike - SMT Oct 15 '19 at 17:08

1 Answers1

1

All I want is a pygame window inside a tkinter window.

I have taken the code you referenced and cleaned it up a bit to better fit Python 3 and PEP8 more closely.

Until you update your question with an example that shows the problem you have described this is the best I can do for you question.

Let me know if you have any questions.

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79