1

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()

It creates 2 windows when I want to create only one. What is going wrong? Thanks!

crackanddie
  • 688
  • 1
  • 6
  • 20
  • I think the answer you linked was for python2. Why do you want to do this though, pygame is not too difficult to make guis in – Rolv Apneseth Jan 15 '21 at 11:49
  • 1
    It doesn't work on pygame 2.x. Use pygame 1.9.x instead. – acw1668 Jan 15 '21 at 11:49
  • @RolvApneseth I have a reason to use only tkinter. There is a textbox in tkinter as I remember where you can highlight text and change its color – crackanddie Jan 15 '21 at 11:51
  • I could not install pygame==1.9.6 using terminal and pycharm. Someone had the same problems: https://stackoverflow.com/questions/50392215/installing-pygame-through-pycharm-or-system-terminal?rq=1. But it didn't help to me. So I am still trying to install it :) – crackanddie Jan 15 '21 at 12:03
  • acw1668 is right, see https://github.com/pygame/pygame/issues/1574 – sloth Jan 15 '21 at 12:04
  • @sloth and what should I do? :) I'm sorry but I really don't understand. The issue is still opened. I tried to install pygame 1.9.6 but only errors occur. I tried pythons 3.6.1, 3.7 and 3.9 and everywhere I couldn't install pygame 1.9.x – crackanddie Jan 15 '21 at 12:30
  • What error when you try to install pygame 1.9.6? Better use Python 3.8 because 1.9.6 has no prebuilt package for Python 3.9. – acw1668 Jan 15 '21 at 12:59
  • it is impossible to fit them two in one window. –  Jan 15 '21 at 13:33
  • @acw1668 EOFError occures. And why it's better to use 3.8 when I even tried 3.6 and 3.7? And I'm going to open new question on stackoverflow because it's another theme. – crackanddie Jan 15 '21 at 14:40
  • How did you install pygame? It is strange to get `EOFError` if using `pip install -U pygame==1.9.6`. Using Python 3.6 and 3.7 is OK as long as there is pre-built package for the version. I have no problem on installing pygame 1.9.6 in my Python 3.8 under Windows 10. And the code works fine in my environment. – acw1668 Jan 15 '21 at 14:45
  • @acw1668 ok, I can try it on 3.8. I opened new question for the problem: https://stackoverflow.com/questions/65738380/errors-occur-when-i-want-to-install-pygame-1-9-x-into-python-3 – crackanddie Jan 15 '21 at 14:57
  • @acw1668 on 3.8 doesn't work too, the same errors – crackanddie Jan 15 '21 at 15:10

1 Answers1

1

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!

crackanddie
  • 688
  • 1
  • 6
  • 20