Pyglet class Window(pyglet.window.Window): doesn't work for me

938 views
Skip to first unread message

Borna Romić

unread,
May 20, 2019, 5:06:22 PM5/20/19
to pyglet-users
For some odd reason 
from pyglet.gl import *
and 
class Window(pyglet.window.Window): doesn't work for me. I always get an error class Window(pyglet.window.Window):
                                                                                                     NameError: name 'pyglet' is not defined
I'm new to Pyglet but I've installed everything correctly, and when I tried to paste a few codes from the internet to see how this all works they all have these 2 lines in the code that for some reason always give me syntax errors.

I could really use some help, it's urgent that I do a program using Pyglet for my lab project but I can't even get it to work properly.

Borna Romić

unread,
May 20, 2019, 5:07:50 PM5/20/19
to pyglet-users
Here's one of many codes I tried copy/pasting that don't work but they should. (found them on youtube)
from pyglet.gl import *

class Triangle:
def __init__(self):
self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.0,0.5,0.0]),
('c3B', [100,200,220, 200,110,100, 100,250,100]))

class MyWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(400, 300)
glClearColor(0.2, 0.3, 0.2, 1.0)

self.triangle = Triangle()

def on_draw(self):
self.clear()
self.triangle.vertices.draw(GL_TRIANGLES)

def on_resize(self, width, height):
glViewport(0, 0, width, height)


if __name__ == "__main__":
window = MyWindow(1280, 720, "My Pyglet Window", resizable=True)
pyglet.app.run()

Charles

unread,
May 20, 2019, 5:39:52 PM5/20/19
to pyglet-users
You need to add
import pyglet

t
o bring pyglet into the namespace.

from pyglet.gl import *

Just brings the OpenGL names into the namespace.
Message has been deleted

Borna Romić

unread,
May 20, 2019, 6:50:31 PM5/20/19
to pyglet-users
This did seem to help, although the code still doesn't work, now I'm met with the message 

UserWarning: Vertex attribute shorthand notation is deprecated: 'v3f'
warnings.warn(message)

I've tried on multiple codes and the same error appears, wonder how it works for other people and not for me? I mean the code is literally the same.

Benjamin Moran

unread,
May 21, 2019, 5:49:27 AM5/21/19
to pyglet-users
That warning message is non-fatal, but it tells me that you're using the shaders branch. Is that what you want? Almost all pyglet examples on the internet will be based on legacy OpenGL, so you are probably better off using pyglet 1.3 or 1.4b unless you really need shader support.

What are your project requirements?

Borna Romić

unread,
May 21, 2019, 6:51:02 AM5/21/19
to pyglet-users
I do need to use shaders for my project, have to make a 3D square which the user is able to rotate among other things, but currently I'm just trying to learn Pyglet through youtube videos but for some reason the codes don't work for me when I copy them into my python (I'm using 3.7). The code in particular I pasted above is supposed to make a triangle in the middle of my screen that is filled with lots of colours and all I get is an error and just a big green screen.

I have pip installed all the required programs 

Honestly, not even sure anymore what the problem is. I can't start learning the program if it won't even work for me. 

Package       Version
------------- -------
future        0.17.1
mpmath        1.1.0
numpy         1.16.3
pip           19.1.1
Polygon3      3.0.8
pyglet        1.4.0b1
pyglet2d      0.2.1
PyOpenGL      3.1.0
PyOpenGL-Demo 3.0.0
scipy         1.2.1
setuptools    41.0.1
sympy         1.4
wheel         0.33.4

Benjamin Moran

unread,
May 21, 2019, 7:33:05 PM5/21/19
to pyglet-users
Except for the missing pyglet import, your code works in 1.3/1.4.
It did not work for the shaders branch. It could be a bug, or something not implemented yet. The shaders branch is still under development. For that reason, and because there is no beginner documentation yet, if you need shaders you should check out ModernGL or PyOpenGL. It's just a little too soon.

Benjamin Moran

unread,
May 22, 2019, 8:25:34 PM5/22/19
to pyglet-users
I noticed what was wrong with your code. pyglet 2.0 (shaders branch) does a lot more in the `on_resize` method. Namely, it sets the default view and projection matrixes in the default shader.
The default shader is fine if you are using window coordinates (0 to 1280, and 0 to 720), but the -1, 1 coordinates won't work because your object is off the left side of the screen. I still do not recommend using this code because of the lack of documentation, but I'll paste some working code below.

Just for general information, you might notice that we're using UniformBufferObjects, so that the "WindowBlock" uniform block can be shared between all Shader Programs. The idea is that you can create your own subclass of pyglet.window.Projection, and assign it to the Window class.The default `Window.on_resize` method sets the projection, therefore updating it for all ShaderPrograms that define the "WindowBlock" Uniform Block.

import pyglet

from pyglet.gl import *

class Triangle:
def __init__(self):
self.vertices = pyglet.graphics.vertex_list(3, ('v3f', [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.0,0.5,0.0]),
('c3B', [100,200,220, 200,110,100, 100,250,100]))

class MyWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_minimum_size(400, 300)
glClearColor(0.2, 0.3, 0.2, 1.0)

self.triangle = Triangle()

def on_draw(self):
self.clear()
self.triangle.vertices.draw(GL_TRIANGLES)

def on_resize(self, width, height):
glViewport(0, 0, width, height)

        identity = (1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0)

with pyglet.graphics.get_default_group().program.uniform_buffers['WindowBlock'] as window_block:
window_block.projection = identity
window_block.view = identity


if __name__ == "__main__":
window = MyWindow(1280, 720, "My Pyglet Window", resizable=True)
pyglet.app.run()
Reply all
Reply to author
Forward
0 new messages