为什么我收到这个班级“未定义”错误? [英] Why am I receiving this class "undefined" error?

查看:72
本文介绍了为什么我收到这个班级“未定义”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿fellas,对社区来说相对较新。我从下一个大学学期开始学习各种编码项目,其中一些涉及接口的开发。我选择PyGTK来完成这项任务 - 我可能会在未来的某个时间试用Glade,看看工作流程是什么样的。



截至目前,我对GUI编码很新,而且我发现自己对PyGTK教程中的这个片段感到非常恼火(请注意,为清楚起见,删除了评论):



#!/ usr / bin / env python 

#example helloworld2。 py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld2:

def callback (自我,小部件,数据):
打印你好再次 - %s被按下%data

def delete_event(self,widget,event,data = None):
gtk.main_quit()
返回False

def __init __(self):
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.set_title (Hello Buttons!)
self.window.connect(delete_event,self.delete_event)
self.window.set_border_width(10)

self.box1 = gtk .HBox(False,0)
self.window.add(self.box1)

self.button1 = gtk.Button(Button 1)
self.button1.connect(clicked,self.callback,button 1)

self.box1.pack_start( self.button1,True,True,0)

self.button1.show()

self.button2 = gtk.Button(Button 2)
self.button2.connect(clicked,self.callback,button 2)

self.box1.pack_start(self.button2,True,True,0)

self.button2.show()
self.box1.show()
self.window.show()

def main():
gtk.main ()

如果__name__ ==__ main__:
hello = HelloWorld2()
main()





我遇到的问题是,一旦我通过我的终端使用chmod a + x HelloWorld.py使其成为可执行文件,一旦尝试它,我就会得到 HelloWorld2无法识别/未定义错误。



有没有人知道为什么会这样?



非常感谢,我希望很快能收到你们的回复。

Akratix

解决方案

好吧,作为一个新手,我犯了一个愚蠢的错误。当if语句的范围内没有定义main时,我试图调用main()。我的修复:



如果__name__ ==__ main__:
hello = HelloWorld2()
hello.main( )





我需要学习很多东西。再次感谢。


Hey fellas, relatively new to the community. I've started off this next college semester with various coding projects, and some of them involve the development of interfaces. I chose PyGTK for such a task -- I may try out Glade some time in the future just to see what the workflow is like.

As of right now, I'm pretty new with GUI coding, and I've found myself rather irritated with this snippet here off of PyGTK's tutorial (note that the comments were removed for clarity):

#!/usr/bin/env python

# example helloworld2.py

import pygtk
pygtk.require('2.0')
import gtk

class HelloWorld2:

    def callback(self, widget, data):
        print "Hello again - %s was pressed" % data

    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Hello Buttons!")
        self.window.connect("delete_event", self.delete_event)
        self.window.set_border_width(10)

        self.box1 = gtk.HBox(False, 0)
        self.window.add(self.box1)

        self.button1 = gtk.Button("Button 1")
        self.button1.connect("clicked", self.callback, "button 1")

        self.box1.pack_start(self.button1, True, True, 0)

        self.button1.show()

        self.button2 = gtk.Button("Button 2")
        self.button2.connect("clicked", self.callback, "button 2")

        self.box1.pack_start(self.button2, True, True, 0)

        self.button2.show()
        self.box1.show()
        self.window.show()

def main():
    gtk.main()

if __name__ == "__main__":
    hello = HelloWorld2()
    main()



The issue I'm having is that, once I put this through my terminal with "chmod a+x HelloWorld.py" to make it an executable, once trying it out, I'm getting the "HelloWorld2" not recognized / undefined error.

Does anyone have any insight as to why this is the case?

Thanks a bunch, and I hope to hear from you guys soon.
Akratix

解决方案

Well, as a newbie, I made a stupid mistake. I was trying to call "main()" when there was no "main" defined within the scope of the if statement. My fix:

if __name__ == "__main__":
    hello = HelloWorld2()
    hello.main()



I've got quite a bit to learn. Thanks again.


这篇关于为什么我收到这个班级“未定义”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆