Warm tip: This article is reproduced from stackoverflow.com, please click
pyopengl python robotics tkinter

Using pyopengltk and add another common tkinter function

发布于 2020-05-04 03:42:46

I currently try to create opengl simulation in tkinter, so i found this. The example code provided there :

import time
import tkinter
from OpenGL import GL
from pyopengltk import OpenGLFrame

class AppOgl(OpenGLFrame):

    def initgl(self):
        """Initalize gl states when the frame is created"""
        GL.glViewport(0, 0, self.width, self.height)
        GL.glClearColor(0.0, 1.0, 0.0, 0.0)    
        self.start = time.time()
        self.nframes = 0

    def redraw(self):
        """Render a single frame"""
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        tm = time.time() - self.start
        self.nframes += 1
        print("fps",self.nframes / tm, end="\r" )


if __name__ == '__main__':
    root = tkinter.Tk()
    app = AppOgl(root, width=320, height=200)
    app.pack(fill=tkinter.BOTH, expand=tkinter.YES)
    app.animate = 1
    app.after(100, app.printContext)
    app.mainloop()

I can run it fine but i sligthly confused because i usually use tkinter with this format :

class frontpage(tk.Tk):
    def __init__(self,parent):
        tk.Tk.__init__(self,parent)
        self.parent=parent

        '''Creating'''
        #Window 1
        label1 = tk.Label(self, text = 'App title',bg='blue',fg='white')
        frame1 = tk.Frame(self)

        ''' Some code '''

        label1.pack(fill='x')
        frame1.pack()

    def func1(self):
        #some function

if __name__ == '__main__':
    frontapp = frontpage(None)
    frontapp.title('App v0.1')
    frontapp.mainloop()

Given that, is there anyway i could insert normal tkinter item such as frame, button, etc using pyopengltk?

Questioner
Albert H M
Viewed
96
community wiki 2020-02-17 21:59

Based on solution suggested by @stovfl, we just need to insert our opengl class into our tkinter class __init__. Then it become :

from __future__ import print_function

import sys, math, time
import tkinter as tk
from OpenGL import GL, GLU
from pyopengltk import OpenGLFrame

class AppOgl(OpenGLFrame):

    def initgl(self):
        """Initalize gl states when the frame is created"""
        GL.glViewport(0, 0, self.width, self.height)
        GL.glClearColor(0.0, 1.0, 0.0, 0.0)    
        self.start = time.time()
        self.nframes = 0

    def redraw(self):
        """Render a single frame"""
        GL.glClear(GL.GL_COLOR_BUFFER_BIT)
        tm = time.time() - self.start
        self.nframes += 1
        print("fps",self.nframes / tm, end="\r" )

class frontpage(tk.Tk):
    def __init__(self,parent):
        tk.Tk.__init__(self,parent)
        self.parent=parent

        open_gl_frame = AppOgl(self, width=320, height=200)
        '''Creating'''
        #Window 1
        label1 = tk.Label(self, text = 'App title',bg='blue',fg='white')
        frame1 = tk.Frame(self)

        ''' Some code '''

        open_gl_frame.animate=1 #dont forget this, or your simulation would not animated
        open_gl_frame.pack()
        label1.pack(fill='x')
        frame1.pack()

    def func1(self):
        pass

if __name__ == '__main__':
        frontapp = frontpage(None)
        frontapp.title('App v0.1')
        frontapp.mainloop()