Code: Alles auswählen
from OpenGL.GL import *
import OpenGL.Tk as togl
import Tkinter as tkinter
import time
class ColorBox(togl.Opengl):
"""A colorful, rotating box"""
def __init__(self, master, cnf=()):
"""Initialize GUI and OpenGL"""
togl.Opengl.__init__(self, master, **cnf)
self.set_eyepoint(20) # move away from object
#total degrees of rotation
self.amountRotated = 0
#rotate amount in degrees
self.increment = 2
def redraw(self, event):
"""Draw box on black background"""
#clear background and disable lighting
glClearColor(0.0, 0.0, 0.0, 0.0) #set clearing color
glClear(GL_COLOR_BUFFER_BIT) #clear background
glDisable(GL_LIGHTING)
#constants
red = (1.0, 0.0, 0.0)
green = (0.0, 1.0, 0.0)
blue = (0.0, 0.0, 1.0)
purple = (1.0, 0.0, 1.0)
vertices =\
[((-3.0, 3.0, -3.0), red),
((-3.0, -3.0, -3.0), green),
(( 3.0, 3.0, -3.0), blue),
(( 3.0, -3.0, -3.0), purple),
(( 3.0, 3.0, 3.0), red),
(( 3.0, -3.0, 3.0), green),
((-3.0, 3.0, 3.0), blue),
((-3.0, -3.0, 3.0), purple),
((-3.0, 3.0, -3.0), red),
((-3.0, -3.0, -3.0), green)]
#begin drawing
glBegin(GL_QUAD_STRIP)
#change color and plot point for each vertex
for vertex in vertices:
location, color = vertex
apply(glColor3f, color)
apply(glVertex3f, location)
#stop drawing
glEnd()
def update(self):
"""Rotate box"""
#change rotation direction
if self.amountRotated >= 500:
self.increment = -2 #rotate left
#change rotation direction
elif self.amountRotated <= 0:
self.increment = 2 #rotate right
#rotate box around x, y, z axis (1.0, 1.0, 1.0)
glRotate(self.increment, 1.0, 1.0, 1.0)
self.amountRotated += self.increment
#redraw geometry
self.tkRedraw()
if __name__ == "__main__":
# Da beim ersten import von OpenGl.Tk ein Tk Fenster gebaut wird zerstoere
# ich dieses hier, da es fuer mich keinen ersichtlichen nutzen hat.
try:
togl._default_root.destroy()
except tkinter.TclError:
pass
root = tkinter.Tk()
root.tk.call('package', 'require', 'Togl')
frame_left = tkinter.Frame(root)
frame_left.pack(side="left")
bt = tkinter.Button(frame_left, text="Do something!")
bt.pack()
cb = ColorBox(root, dict(double=1))
cb.pack(expand="yes", fill="both")
while True:
time.sleep(0.025)
cb.update()
root.update()
root.mainloop()
übernommen und in einen Funktionierenden Context gesetzt.
Edit: Den Code nochmal etwas verbessert.