Pygtk:名称未定义
我正在尝试一些 pygtk 教程,并遇到了一个看似明显的新手错误,但我一生都无法弄清楚这里发生了什么。
错误:
Traceback (most recent call last): File "main.py", line 8, in class Base: File "main.py", line 61, in Base cv.set_line_width(9) NameError: name 'cv' is not defined
代码:
def expose(self, widget, data=None):
cv = widget.window.cairo_create()
cv.set_line_width(9)
cv.set_source_rgb(0.7, 0.2, 0.0)
w = self.window.allocation.width
h = self.window.allocation.height
cv.translate(w/2, h/2)
cv.arc(0, 0, 50, 0, 2*math.pi)
cv.stroke_preserve()
cv.set_source_rgb(0.3, 0.4, 0.6)
cv.fill()
这是完整的源代码: http://gist.github.com/655728
I'm trying out a few pygtk tutorials and have run across a seemingly obvious newbie mistake, but for the life of me can't figure out what's going on here.
The error:
Traceback (most recent call last): File "main.py", line 8, in class Base: File "main.py", line 61, in Base cv.set_line_width(9) NameError: name 'cv' is not defined
The code:
def expose(self, widget, data=None):
cv = widget.window.cairo_create()
cv.set_line_width(9)
cv.set_source_rgb(0.7, 0.2, 0.0)
w = self.window.allocation.width
h = self.window.allocation.height
cv.translate(w/2, h/2)
cv.arc(0, 0, 50, 0, 2*math.pi)
cv.stroke_preserve()
cv.set_source_rgb(0.3, 0.4, 0.6)
cv.fill()
Here is the full source: http://gist.github.com/655728
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在 github 中的代码如下:
...这肯定可以解释为什么当您尝试访问它时 cv 没有定义。
Your code in github reads:
...which would surely explain why cv is not defined when you try to access it.
您的文件中混合有制表符和空格。
You have a mixture of tabs and spaces in your file.
通过切换到 4 个空格缩进而不是制表符并重新缩进整个文件,解决了这个问题。
发生了一些奇怪的事情,gedit 显示了所有缩进的内容,而 Netbeans 显示了缩进的罪魁祸首
This was solved by switching to 4-space indents instead of tabs and re-indenting the entire file.
Something weird was going on, gedit was showing everything nicely indented while Netbeans showed the indent culprit