0

I was implementing simple text translation using pyglet. It works perfectly when there is no config added in window = pyglet.window.Window(). However, the code does not run after config is added in line 8. I am using 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()

1 Answers1

0

Try to define the config from your window config, Try this:

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()
Omi Harjani
  • 737
  • 1
  • 8
  • 20
  • Thanks, your code works.But can you explain why my previous code was not working? Shouldn't `get_best_config()` should give me the right `config` object. – Arihant Parsoya Feb 26 '18 at 10:12
  • python interpreter parses top to bottom, because of which when config is defined, template definition does not receive config value. one thing you may do to make your code work is redefine config = get_best_config() before template that will make your code work as intended. Please check, i have edited the answer. – Omi Harjani Feb 26 '18 at 10:21
  • Okay, in the documentation they have done it other way round: http://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/programming_guide/context.html?highlight=pyglet.gl.Config#simple-context-configuration they used the `template` to define the `config` – Arihant Parsoya Feb 26 '18 at 10:53