LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Python: Global Name Not Defined (http://www.linuxquestions.org/questions/programming-9/python-global-name-not-defined-886984/)

MetaMan 06-17-2011 11:19 PM

Python: Global Name Not Defined
 
1 Attachment(s)
OK, I found an interesting screencast online about how to make gtk Pyton apps. The thing is, though, the guy was using the interactive shell. I've been trying to get his code into a script, and have been having troubles.

Here's what I got:
Code:

#!/usr/bin/env python

#Text Variables
title = "SimpleBrowser"

import gtk
import webkit
class Application:
        def destroy(self, window):
                gtk.main_quit()
               
        def __init__(self):
                window = gtk.Window()
                go = gtk.Button("Go!")
                hbox = gtk.HBox()
                vbox = gtk.VBox()
                text = gtk.Entry()
                browser = webkit.WebView()
                scrolledwindow = gtk.ScrolledWindow()

                scrolledwindow.add(browser)
                hbox.pack_start(text)
                hbox.pack_start(go)
                vbox.pack_start(hbox)
                vbox.pack_start(scrolledwindow)
                window.set_size_request(400, 300)
                window.set_title(title)
                window.connect("destroy", self.destroy)
                go.connect("clicked", self.goclicked)
                window.add(vbox)
                window.show_all()

        def goclicked(self, btn):               
                browser.open(text.get_text())


if __name__ == "__main__":
        application = Application()
        gtk.main()

It spits out the error "NameError: global name 'browser' is not defined"

I know I'm doing something wrong with how I'm telling it where to find "browser" and "text", but I can't figure out how to point it to the right place.

Anyone know how to fix this? Feel free to point out other ways to improve it.

Thanks! :D

paulsm4 06-18-2011 12:32 AM

Hi -

Well, like "browser" isn't defined inside of goclicked(), is it ;)?

Suggestion - declare it as a global. Here's one (probably not very good :)) example:
Code:

#!/usr/bin/env python

#Text Variables
title = "SimpleBrowser"

import gtk
import webkit

browser = webkit.WebView()

class Application:
        def destroy(self, window):
                gtk.main_quit()
        ...
        def __init__(self):
                window = gtk.Window()
                go = gtk.Button("Go!")
                hbox = gtk.HBox()
                vbox = gtk.VBox()
                text = gtk.Entry()
                scrolledwindow = gtk.ScrolledWindow()
                scrolledwindow.add(browser)
        ...
        def goclicked(self, btn):               
                browser.open(text.get_text())
        ...


MTK358 06-18-2011 08:14 AM

Quote:

Originally Posted by paulsm4 (Post 4389083)
Hi -

Well, like "browser" isn't defined inside of goclicked(), is it ;)?

Suggestion - declare it as a global. Here's one (probably not very good :)) example:

That's not a good solution. Why not use instance varaibles?

Code:

#!/usr/bin/env python

#Text Variables
title = "SimpleBrowser"

import gtk
import webkit
class Application:
        def destroy(self, window):
                gtk.main_quit()
               
        def __init__(self):
                self.window = gtk.Window()
                self.go = gtk.Button("Go!")
                self.hbox = gtk.HBox()
                self.vbox = gtk.VBox()
                self.text = gtk.Entry()
                self.browser = webkit.WebView()
                self.scrolledwindow = gtk.ScrolledWindow()

                scrolledwindow.add(browser)
                hbox.pack_start(text)
                hbox.pack_start(go)
                vbox.pack_start(hbox)
                vbox.pack_start(scrolledwindow)
                window.set_size_request(400, 300)
                window.set_title(title)
                window.connect("destroy", self.destroy)
                go.connect("clicked", self.goclicked)
                window.add(vbox)
                window.show_all()

        def goclicked(self, btn):             
                self.browser.open(self.text.get_text())


if __name__ == "__main__":
        application = Application()
        gtk.main()


MetaMan 06-18-2011 10:02 AM

Thanks! I'm still learn this stuff, I know I'm a noob. This will help me a lot in the future! :D
I tried the "self.browser.open", but didn't think that I'd have to use self when I first declared them.

Anyway, once again, thank you! :D

MetaMan 06-18-2011 10:07 AM

One more question...
 
Is "self" just a shortcut to it's own class? So self.something instead of It'sOwnClass.something? I've been thinking that so far, so correct me if I'm wrong.

MTK358 06-18-2011 10:23 AM

Quote:

Originally Posted by MetaMan (Post 4389332)
Is "self" just a shortcut to it's own class? So self.something instead of It'sOwnClass.something? I've been thinking that so far, so correct me if I'm wrong.

It's a shortcut to the current instance of the class, not the class itself. There is always exactly one class, but there can be zero or more instances of it.

And "self" is not really a "shortcut", it is the instance. Almost all object-oriented languages work this way (by having the methods belong to the class, and passing the instance as the first parameter), but most of them hide this.

Basically, this:

Code:

instance.method(a, b)
is the same as this:

Code:

instance.__class__.method(instance, a, b)
.


All times are GMT -5. The time now is 10:46 PM.