我发现这个Google groups discussion有关如何在pyqtgraph的GLViewWidget
中显示文字。我希望能够更改显示的文本,所以我修改了代码:
import pyqtgraph.opengl as gl
from PyQt4.Qt import QApplication
class MyGLView(gl.GLViewWidget):
def paintGL(self, *args, **kwds):
global text
gl.GLViewWidget.paintGL(self, *args, **kwds)
self.renderText(0, 0, 0, text)
app = QApplication([])
w = MyGLView()
w.show()
text = "123"
w.renderText(0, 0, 0, text)
这可能不是最好的方法。是否可以重写此代码以避免使用全局变量text
?
答案 0 :(得分:2)
如果您希望text
脱离全局命名空间,请将其设为MyGLView
类的属性。例如:
import pyqtgraph.opengl as gl
from PyQt4.Qt import QApplication
class MyGLView(gl.GLViewWidget):
def __init__(self, text):
super(MyGLView, self).__init__()
self.text = text
def setText(self, text):
self.text = text
self.update()
def paintGL(self, *args, **kwds):
gl.GLViewWidget.paintGL(self, *args, **kwds)
self.renderText(0, 0, 0, self.text)
app = QApplication([])
w = MyGLView(text="O HAI World")
w.show()