py-sdl / py-sdl2

Python ctypes wrapper around SDL2
Other
304 stars 49 forks source link

SDL_CreateWindowFrom gives sigsegv, how to resolve? #176

Closed zejunli closed 2 years ago

zejunli commented 3 years ago

Here is the code, I'm trying to embed sdl2 window into Tkinter window, but CreateWindowFrom causes the program to crash

import tkinter as tk
from tkinter import *
from sdl2 import *
import ctypes

print('0')

root = tk.Tk();
root.geometry('1500x700')

embed = tk.Frame(root, width = 800, height = 400)
embed.pack(side=LEFT)

SDL_Init(SDL_INIT_VIDEO)
print('1')
window = SDL_CreateWindowFrom(str(embed.winfo_id()))
print('2')

ren = SDL_CreateRenderer(window, -1, 0)
print('3')

SDL_SetRenderDrawColor(ren, ctypes.c_utype(255), ctypes.c_utype(20),
                       ctypes.c_utype(147), ctypes.c_utype(255))
print('4')

SDL_RenderClear(ren)
print('5')

while True:
    SDL_RenderPresent(ren)
    root.update()
    print('loop')

Also, is it possible to embed a c++ sdl2 window into python tkinter window? Here is the reference: https://stackoverflow.com/questions/54404324/how-can-i-embed-an-sdl2-window-in-my-tkinter-gui-application

a-hurst commented 3 years ago

@zejunli I'll take a look at this, but in general my experience has been that SDL2 and tkinter don't get along too well, sometimes in weird ways. Last time I tried to use the two together was a few years back, where using a Tk-created window instead of an SDL2-created one resulted in my program segfaulting whenever I tried to quit, but only on some versions of some OSes. After that experience I've adopted a pure-SDL2 windowing approach wherever possible.

I'm sure you've got a good reason, but is there a specific practical reason SDL2's windowing won't work for your use-case? Only asking because my reason for Tk was because I didn't realize SDL2 also offered resolution detection, so maybe you might be in a similar boat.

As for C++ embedding, I'm guessing you'd want to take a look at Pybind11 or maybe Cython (the former has better C++ support, the latter is more popular) to make a Python API for your C code. No clue if it's possible to pass windowing off to Python's Tk with that method, though.

zejunli commented 3 years ago

@zejunli I'll take a look at this, but in general my experience has been that SDL2 and tkinter don't get along too well, sometimes in weird ways. Last time I tried to use the two together was a few years back, where using a Tk-created window instead of an SDL2-created one resulted in my program segfaulting whenever I tried to quit, but only on some versions of some OSes. After that experience I've adopted a pure-SDL2 windowing approach wherever possible.

I'm sure you've got a good reason, but is there a specific practical reason SDL2's windowing won't work for your use-case? Only asking because my reason for Tk was because I didn't realize SDL2 also offered resolution detection, so maybe you might be in a similar boat.

As for C++ embedding, I'm guessing you'd want to take a look at Pybind11 or maybe Cython (the former has better C++ support, the latter is more popular) to make a Python API for your C code. No clue if it's possible to pass windowing off to Python's Tk with that method, though.

Hi Austin, thanks for your reply, so what's going on here is me my team has a game engine developed in pure C++ using sdl2, we have also used pybind to build the engine into a python lib, so I initially was trying to fit this c++ sdl2 window somehow into a python GUI library window, where the python part will be a wrap-around panel that supports editing the game, i.e. adding stuff to the game using our engine API,I tried kivy and tkinter and both didn't end up having good result, so now we have to forsake this plan, we're trying to create another window to act like a control panel, I think embedding sdl2 into tkinter will lead us to more uncontrollable problems so that's also a part we consider, but this makes me wonder how those fancy GUI apps are developed, for instance VsCode and Sublime Text, they obviously have tons of control units in the GUI, what kind of lib/framework do they use?

a-hurst commented 3 years ago

@zejunli Your use of C++ might complicate things, but for the bare minimum of getting your C++ SDL2 code running through Python you can check out the PyBoy project for inspiration, which is a Python-based GameBoy emulator that I know uses compiled Cython SDL2 for windowing.

To my knowledge VSCode is built on Electron (which I think uses Chromium as a base) and TypeScript, so they're basically running a full web browser in an app to give you that kind of UI flexibility. For your use case you might want to give Qt5/PtQT5 or PySide a look, they offer a lot of flexibility for the kind of use case I think you're describing!

a-hurst commented 3 years ago

Just looked at this again: you may have figured out a different solution already, but it looks like SDL_CreateWindowFrom takes ctypes pointer as an argument (pointing to platform-dependent window creation data), whereas your code is passing a string? That might be part of the problem.