LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-17-2011, 11:19 PM   #1
MetaMan
Member
 
Registered: Apr 2011
Distribution: Arch Linux :D
Posts: 50

Rep: Reputation: 0
Question Python: Global Name Not Defined


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!
Attached Files
File Type: txt app.py.txt (808 Bytes, 3 views)
 
Old 06-18-2011, 12:32 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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())
        ...
 
Old 06-18-2011, 08:14 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by paulsm4 View Post
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()
 
Old 06-18-2011, 10:02 AM   #4
MetaMan
Member
 
Registered: Apr 2011
Distribution: Arch Linux :D
Posts: 50

Original Poster
Rep: Reputation: 0
Thanks! I'm still learn this stuff, I know I'm a noob. This will help me a lot in the future!
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!
 
Old 06-18-2011, 10:07 AM   #5
MetaMan
Member
 
Registered: Apr 2011
Distribution: Arch Linux :D
Posts: 50

Original Poster
Rep: Reputation: 0
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.
 
Old 06-18-2011, 10:23 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714Reputation: 714
Quote:
Originally Posted by MetaMan View Post
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)
.
 
  


Reply

Tags
classes, gtk, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Global Variable? ElementNine Programming 5 11-05-2007 01:53 PM
Python: making a reference to a function not yet defined JRR883 Programming 1 02-08-2006 12:28 PM
Python ignores global statement? voyciz Programming 3 04-08-2005 03:07 PM
python global variable? goestin Programming 1 01-20-2005 07:52 AM
global const int X = DEFINED (shows value in debugger?) abs Programming 2 09-25-2004 12:09 PM


All times are GMT -5. The time now is 05:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration