0

所以我刚开始在 python 中编写 GUI,在 tkinter 中编写了一些东西之后,我觉得我还不如使用 imgui,因为我从 C++ 中了解了一些关于它的东西。

现在开始阅读文档,不知何故 imgui 窗口没有出现。我只看到一个控制台弹出一毫秒但没有 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()

我觉得我没有选择任何引擎来渲染,但我不确定。

4

1 回答 1

1

是的,如果你不为 pyimgui 选择后端渲染器,它不会渲染任何东西。我想您可能会在当前目录中找到一个 imgui.ini 文件,其中包含 imgui 对您所要求的内容的解释,但您不会得到任何图形输出。

要获得图形输出,请选择一个渲染器,例如在主 pyimgui 存储库中的此文件中: https ://github.com/swistakm/pyimgui/blob/master/doc/examples/integrations_pygame.py

请注意,在我写这篇文章时,此目录中的大多数但不是所有示例都有效: https ://github.com/swistakm/pyimgui/tree/master/doc/examples

“一体式”示例对我不起作用,而 SDL2 示例需要更改:

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 16)

至:

SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4)

因为我的 GPU 似乎没有进行 16 点多重采样。

此外,这些示例不能很好地与 Python3 配合使用,因为错误 prints() 是错误的,并且会失败而不是打印有用的错误:

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

应该:

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

因此,如果您在这些行中遇到任何错误,您就知道该怎么做。:-)

最后,对于未来的搜索者,这里是对我有用的 pygame 版本的快速剪切和粘贴(来自https://github.com/swistakm/pyimgui/blob/master/doc/examples/integrations_pygame.py)。我用 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()
于 2019-11-29T22:27:19.037 回答