0

So I just started coding GUI in python and after coding some stuff in tkinter i felt like I might as well use imgui as I know some stuff about it from C++.

Now starting to read the docs and somehow the imgui window isn't showing up. I just see a console pop open for a milisecond but no gui.


# initilize imgui context (see documentation)
imgui.create_context()
imgui.get_io().display_size = 100, 100
imgui.get_io().fonts.get_tex_data_as_rgba32()

# start new frame context
imgui.new_frame()

# open new window context
imgui.begin("Your first window!", True)

# draw text label inside of current window
imgui.text("Hello world!")

# close current window context
imgui.end()

# pass all drawing comands to the rendering pipeline
# and close frame context
imgui.render()
imgui.end_frame()

I feel like I didn't choose any engine to render but I'm not sure.

Moog
  • 10,193
  • 2
  • 40
  • 66
3 agle
  • 1
  • 1
  • 4

1 Answers1

1

Yes, if you don't select a backend renderer for pyimgui, it won't render anything. I think you might find an imgui.ini file with imgui's interpretation of what you've asked for, in the current directory, but you won't get any graphical output.

To get graphical output, select a renderer, like in this file in the master pyimgui repository: https://github.com/swistakm/pyimgui/blob/master/doc/examples/integrations_pygame.py

Note that, as I write this, most but not all of the examples in this directory work: https://github.com/swistakm/pyimgui/tree/master/doc/examples

The "all in one" example did not work for me, and the SDL2 example required changing:

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16)

to:

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4)

as my GPU doesn't seem to do 16-point multisampling.

Also, these examples don't play well with Python3 in that the error prints() are wrong, and will fail instead of printing a useful error:

print("Error: SDL could not initialize! SDL Error: " + SDL_GetError())

should be:

print("Error: SDL could not initialize! SDL Error: ", SDL_GetError())

So, if you get any errors at these lines, you know what to do. :-)

Finally, for future searchers, here is a quick cut and paste of the pygame version which works for me (from https://github.com/swistakm/pyimgui/blob/master/doc/examples/integrations_pygame.py ). I ran it with python 3.6:

#!/usr/bin/env python3
from __future__ import absolute_import

import sys

import pygame
import OpenGL.GL as gl

from imgui.integrations.pygame import PygameRenderer
import imgui


def main():
    pygame.init()
    size = 800, 600

    pygame.display.set_mode(size, pygame.DOUBLEBUF | pygame.OPENGL | pygame.RESIZABLE)

    imgui.create_context()
    impl = PygameRenderer()

    io = imgui.get_io()
    io.display_size = size

    while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            impl.process_event(event)

        imgui.new_frame()

        if imgui.begin_main_menu_bar():
            if imgui.begin_menu("File", True):

                clicked_quit, selected_quit = imgui.menu_item(
                    "Quit", 'Cmd+Q', False, True
                )

                if clicked_quit:
                    exit(1)

                imgui.end_menu()
            imgui.end_main_menu_bar()

        imgui.show_test_window()

        imgui.begin("Custom window", True)
        imgui.text("Bar")
        imgui.text_colored("Eggs", 0.2, 1., 0.)
        imgui.end()

        # note: cannot use screen.fill((1, 1, 1)) because pygame's screen
        #       does not support fill() on OpenGL sufraces
        gl.glClearColor(1, 1, 1, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)
        imgui.render()
        impl.render(imgui.get_draw_data())

        pygame.display.flip()


if __name__ == "__main__":
    main()
Ray
  • 11
  • 1
  • Thanks now i noticed, that i cannot render pyimgui itself. Is there a way to render only the imgui or attach imgui to a dynamic window that automatically moves and resizes when imgui gets bigger (drop down Menu for example)? – 3 agle Dec 14 '19 at 20:13