Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 25-Aug-14 6:21am
v2

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900