Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用pysdl2 Python绑定调用sdlttf.TTF_OpenFont()时键入错误_Python_Sdl 2_Sdl Ttf - Fatal编程技术网

使用pysdl2 Python绑定调用sdlttf.TTF_OpenFont()时键入错误

使用pysdl2 Python绑定调用sdlttf.TTF_OpenFont()时键入错误,python,sdl-2,sdl-ttf,Python,Sdl 2,Sdl Ttf,我已正确安装了所有依赖项(SDL2、SDL2_TTF、pysdl2)。 我尝试只提供字体的文件名,并尝试硬编码完整路径。该字体与python文件位于同一目录中 import sys try: from sdl2 import * import sdl2.ext as sdl2ext import sdl2.sdlttf as sdlttf except ImportError: import traceback traceback.print_exc()

我已正确安装了所有依赖项(SDL2、SDL2_TTF、pysdl2)。 我尝试只提供字体的文件名,并尝试硬编码完整路径。该字体与python文件位于同一目录中

import sys
try:
    from sdl2 import *
    import sdl2.ext as sdl2ext
    import sdl2.sdlttf as sdlttf
except ImportError:
    import traceback
    traceback.print_exc()
    sys.exit(1)


def run():
    sdl2ext.init()
    sdlttf.TTF_Init()
    RESOURCES = sdl2ext.Resources(__file__, "")
    # print(RESOURCES.get_path("arial.ttf"))
    font = sdlttf.TTF_OpenFont(RESOURCES.get_path("arial.ttf"), 24)
    window = sdl2ext.Window("SDL_TTF test", size=(800, 600))
    message = sdlttf.TTF_RenderText_Solid(font, "Hello World", (255, 255, 255))
    window.show()
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
        window.refresh()
    return 0

if __name__ == "__main__":
    sys.exit(run())
返回:

Traceback (most recent call last):
  File "sdl2_test.py", line 32, in <module>
    sys.exit(run())
  File "sdl2_test.py", line 17, in run
    font = sdlttf.TTF_OpenFont(RESOURCES.get_path("arial.ttf"), 24)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
回溯(最近一次呼叫最后一次):
文件“sdl2_test.py”,第32行,在
sys.exit(run())
运行中的第17行文件“sdl2_test.py”
font=sdlttf.TTF\u OpenFont(RESOURCES.get\u path(“arial.TTF”),24)
ctypes.ArgumentError:参数1::错误类型

我明白了。因为我使用的是Python3,所以该类型不正确,因为Python3中用于表示字符串的标准类型。我用str.encode()解决了这个问题,我明白了。因为我使用的是Python3,所以该类型不正确,因为Python3中用于表示字符串的标准类型。我用str.encode()解决了这个问题