使用 pyopengltk 并添加另一个常用的 tkinter 函数
Using pyopengltk and add another common tkinter function
我目前尝试在 tkinter 中创建 opengl 模拟,所以我找到了 this。那里提供的示例代码:
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()
我可以 运行 很好,但我有点困惑,因为我通常使用这种格式的 tkinter :
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()
鉴于此,我是否可以使用 pyopengltk 插入正常的 tkinter 项目,例如框架、按钮等?
根据@stovfl 建议的解决方案,我们只需要将我们的 opengl class 插入到我们的 tkinter class __init__
中。然后它变成:
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()
我目前尝试在 tkinter 中创建 opengl 模拟,所以我找到了 this。那里提供的示例代码:
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()
我可以 运行 很好,但我有点困惑,因为我通常使用这种格式的 tkinter :
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()
鉴于此,我是否可以使用 pyopengltk 插入正常的 tkinter 项目,例如框架、按钮等?
根据@stovfl 建议的解决方案,我们只需要将我们的 opengl class 插入到我们的 tkinter class __init__
中。然后它变成:
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()