electronstudio / raylib-python-cffi

Python CFFI bindings for Raylib
http://electronstudio.github.io/raylib-python-cffi
Eclipse Public License 2.0
142 stars 29 forks source link

How do you get raygui in pyray? #111

Closed drakbar closed 9 months ago

drakbar commented 9 months ago

looking at the documentation and readme at the base of the repo, it says that gui functions should be available. I have tested the following code using both pypy and cpython both yield the same result. I am not sure how this python bindings work, but I assume that the symbols are loaded in from querying a list of symbols in a shared object at runtime. If so that would imply raygui functions are not in the .so on my system. Which I thought that raygui was header only. Would love to attempt to use this binding but really want to explore the entire eco-system.

here is the code that attempts to use raygui

import pyray as pr

char_array = pr.ffi.new("char[20]")
pointer = pr.ffi.addressof(char_array)

pr.init_window(800, 600, "raygui")
while not pr.window_should_close():
    pr.begin_drawing()
    pr.clear_background(pr.RAYWHITE)

    pr.gui_text_input_box(pr.Rectangle(0, 0, 800, 600), "title", "message", "buttons", pointer)
    string = pr.ffi.string(char_array).decode('utf-8')
    pr.draw_text(f"Text entered: {string}", 10, 40, 10, pr.DARKGRAY)

    pr.end_drawing()
pr.close_window()
AttributeError: module 'pyray' has no attribute 'gui_text_input_box'
sDos280 commented 9 months ago

that "works" for me, What version of pyray are you using? and how did you install pyray? make sure to use the latest one.

p.s. the gui_text_input_box takes 7 params and not just 5.

drakbar commented 9 months ago

pip3 install raylib was the method that used to install pyray. pip3 freeze claims that I am using raylib==4.5.0.0

That example code was something I pulled from another issue in this repo. When ever I import pyray it does dump this to the terminal as well

>>>> import pyray
RAYLIB STATIC 4.5.0.0 LOADED

Now the only caveat to my setup in that I am running in a virtual environment, which did complain a lot when trying to pip install it.

sDos280 commented 9 months ago

strange, that works (for me) in raylib 4.5.0.0 . Try to reinstall.

p.s. Is there a reason why you are using a virtual environment? maybe that is the problem...

drakbar commented 9 months ago

Yeah the whole reason for VE is so that different versions of libraries can be installed on your system without conflicting with one another and polluting you system libraries.

It is a very crude method of isolation in order to "reproduce" a effect. A low effort attempt for something a kin to a scientific control. VE are a lighter weight / faster approach to say something like a docker container.

If you are not using virtual environments for code exploration you should really try it out. It doesn't provide you with any additional layer of security, just a method of managing dependencies.

electronstudio commented 9 months ago

What’s the OS and Python version?

drakbar commented 9 months ago

Ubuntu 22.04 python 3.10

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux

pypy

Python 3.10.12 (af44d0b8114cb82c40a07bb9ee9c1ca8a1b3688c, Jun 15 2023, 12:39:27)
[PyPy 7.3.12 with GCC 10.2.1 20210130 (Red Hat 10.2.1-11)] on linux
electronstudio commented 9 months ago

First, as sDos280 said, "takes 7 params and not just 5" so you will need to change that line to:

 pr.gui_text_input_box(pr.Rectangle(0, 0, 800, 600), "title", "message", "buttons", pointer, 10, 0)

However, that won't solve your problem about Raygui not being found.

When you install raylib-python-cffi using pip, the binary wheel that should install is this one:

https://files.pythonhosted.org/packages/92/d4/3c5358492aca4a95a54b73db12f1b91ff923bc23af311e707d9621b21e8a/raylib-4.5.0.0-cp310-cp310-manylinux2014_x86_64.whl

I've tested this and raygui works with it.

It's possible that for some reason pip decided to downloaded the source tarball and compile it rather than using the binary. In that case you will need to ensure that raygui.h is somewhere on the include path at the time the compile happens, e.g:

sudo cp raygui/src/raygui.h /usr/local/include/

Note if you reinstall using pip, it will probably use the cached version it compiled previously, so you will need to force it to recompile. Probably this will do that:

pip3 install --no-cache-dir --no-binary raylib --upgrade --force-reinstall raylib==4.5.0.0
electronstudio commented 9 months ago

actually /usr/local/include is just my include path, you need to check what yours is like this:

pkg-config --variable=includedir raylib

and then copy raygui.h to there.

(Or just install the binary wheel I linked above, probably easier.)

drakbar commented 9 months ago

@electronstudio

That was it! Your suspicion was right. I remember when I first attempted to install raylib via pip it complained. So I built and installed the shared object from source system wide. Only after that did the pip install complete successfully.

I do know that at the time of the initial successful pip install raygui.h was not present at /usr/local/include. Since it was in a virtual env I don't know if the cached version that you mentioned was still floating around. The forced no cache reinstall did work however.

@electronstudio Thank you for your help!