Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a Pygame window into a Tkinter or WxPython frame

Tags:

A friend and I are making a game in pygame. We would like to have a pygame window embedded into a tkinter or WxPython frame, so that we can include text input, buttons, and dropdown menus that are supported by WX or Tkinter. I have scoured the internet for an answer, but all I have found are people asking the same question, none of these have been well answered.

What would be the best way implement a pygame display embedded into a tkinter or WX frame? (TKinter is preferable)

Any other way in which these features can be included alongside a pygame display would also work.

like image 473
AHuman Avatar asked Apr 27 '14 03:04

AHuman


People also ask

Can I combine pygame and tkinter?

PYGAME and TKINTER in HARMONY. Despite what is said on some forums, Tkinter, the middle weight python GUI, works quite well in conjunction with pygame, the python SDL layer - at least when pygame is not in full screen mode.

Which is better pygame or tkinter?

First, tkinter is definitely not the best GUI toolkit for python. It's one of the simplest and usually comes with the python interpreter. But it's not as powerful as Qt, wx or Gtk. pygame is - as it's name states - a package designed to allow to create games in python very easily, not to create GUIs.

What is the difference between tkinter window and frame?

Tk creates the root window. Every tkinter application must have a root window. When you instantiate it you also create a tcl interpreter that is used by tkinter. Frame is just a widget, designed to be a container for other widgets.


1 Answers

(Note this solution does not work on Windows systems with Pygame 2. See Using 'SDL_WINDOWID' does not embed pygame display correctly into another application #1574. You can currently download older versions of Pygame here.)

According to this SO question and the accepted answer, the simplest way to do this would be to use an SDL drawing frame.

This code is the work of SO user Alex Sallons.

import os import pygame import Tkinter as tk from Tkinter import *  root = tk.Tk() embed = tk.Frame(root, width = 500, height = 500) #creates embed frame for pygame window embed.grid(columnspan = (600), rowspan = 500) # Adds grid embed.pack(side = LEFT) #packs window to the 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(255,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() 

This code is cross-platform, as long as the windib SDL_VIDEODRIVER line is omitted on non Windows systems. I would suggest

# [...] import platform if platform.system == "Windows":     os.environ['SDL_VIDEODRIVER'] = 'windib' # [...] 
like image 140
PythonNut Avatar answered Oct 08 '22 04:10

PythonNut