【问题标题】:pyglet not running as expected in MacOSpyglet 在 MacOS 中未按预期运行
【发布时间】:2018-08-05 15:12:11
【问题描述】:

我正在使用 pyglet 实现简单的文本翻译。当window = pyglet.window.Window() 中没有添加config 时,它可以完美运行。但是,在第 8 行添加 config 后代码无法运行。我使用的是 Mac High Sierra。

import pyglet

platform = pyglet.window.get_platform()
display = platform.get_default_display()
screen = display.get_default_screen()
template = pyglet.gl.Config()
config = screen.get_best_config(template)
window = pyglet.window.Window(config=config)
label = pyglet.text.Label('Hello, world', x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')

def update(dt):
  #print(dt) # time elapsed since last time we were called
  label.x += 1
  label.y += 1

@window.event
def on_draw():
  window.clear()
  label.draw()

pyglet.clock.schedule(update) # cause a timed event as fast as you can!
pyglet.app.run()

【问题讨论】:

    标签: python macos pyglet pyopengl


    【解决方案1】:

    尝试从您的窗口配置中定义配置,试试这个:

    import pyglet
    window = pyglet.window.Window()
    context = window.context
    #config = context.config
    platform = pyglet.window.get_platform()
    display = platform.get_default_display()
    screen = display.get_default_screen()
    config = screen.get_best_config()#if you need best config although if you add template as an argument it forms a deadlock.
    template = pyglet.gl.Config(config=config)
    #config = screen.get_best_config(template)
    label = pyglet.text.Label('Hello, world', x=window.width//2, y=window.height//2,
                              anchor_x='center', anchor_y='center')
    
    def update(dt):
      #print(dt) # time elapsed since last time we were called
      label.x += 1
      label.y += 1
    
    @window.event
    def on_draw():
      window.clear()
      label.draw()
    
    pyglet.clock.schedule(update) # cause a timed event as fast as you can!
    pyglet.app.run()
    

    【讨论】:

    • 谢谢,您的代码有效。但是您能解释一下为什么我之前的代码无效吗? get_best_config() 不应该给我正确的config 对象。
    • python解释器从上到下解析,因此在定义配置时,模板定义没有接收到配置值。为了使代码正常工作,您可以做的一件事是在模板之前重新定义 config = get_best_config() ,这将使您的代码按预期工作。请检查,我已经编辑了答案。
    • 好的,在文档中,他们以另一种方式进行:pyglet.readthedocs.io/en/pyglet-1.3-maintenance/… 他们使用template 来定义config
    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 2020-06-28
    • 2012-02-18
    • 2018-01-18
    • 2012-06-14
    相关资源
    最近更新 更多