Hi im working on an encyclopedia. The problem im having is the following:

in next
print car_num, len (car_list)
NameError: global name 'car_list' is not defined
Line 62

I might also add that before i moved car_list into a separate script/module, everything worked fine. The idea is basically to have a very long list of information that one could page through but i would like to keep it separate from the actual code for possible future expansion.Thanks in advance.

# My encyclopedia.
# An interactive encyclopedia which shows
# pictures and information about them.

import Tkinter as tk
from PIL import ImageTk, Image
from car_list  import *

root = tk.Tk()
root.title("Encyclopedia")


class Car(object):
    """this class creates a record structure"""
    def __init__(self, title, info, image):
        self.title = title
        self.info = info
        self.image = image
        

def main():
    global car_num
    
    car_num = 0
    create_widgets()
    root.mainloop()

def create_widgets():
    """ create text area and buttons"""
    global screen
    global info
    
    # text area for pictures
    screen = tk.Text(root, width=40, height=13, bg="Grey")
    screen.pack(side='top', expand='yes', fill='both')
    screen.insert(0.0, '\n\n\t Welcome to the Golfs history\n\n')
    
    # text area for information about pictures
    info = tk.Text(root, width=40, height=8, bg="White")
    info.pack(side='top', expand='yes', fill='both')
    
    # previous button
    prev = tk.Button(root, width=20, text="Previous", fg="Blue",
        command=previous)
    prev.pack(side='left')

    # next buttton
    nexst = tk.Button(root, width=20, text="Next", fg="Blue", 
        command=next)
    nexst.pack(side='left', fill='x')

    # quit button
    quit = tk.Button(root, width=20, text="Quit", fg="Blue",
        command=root.destroy)
    quit.pack(side='right', expand='yes')

def next():
    global car_num
    global car_list
    
    print car_num, len (car_list)

    show(car_num)
     #increment to next car number
    if car_num < len(car_list)- 1:
        car_num += 1

def previous():
    global car_num
    
    # decrement to previous car number
    if car_num > 0:
        car_num -= 1
    show(car_num)

def show(car_num):
    global car_list
    global screen
    global info    
    
    # delete any previous picture, title and info
    screen.delete(0.0, 'end')
    info.delete(0.0, 'end')
    # create picture heading
    s = car_list[car_num].title + '\n\n'
    screen.insert('1.0', s)
    # insert picture
    screen.image_create('2.0', image=car_list[car_num].image)
    # insert information about the picture
    info.insert('1.0', car_list[car_num].info)

main()

From what I can tell, you don't have a main-scope variable named car_list within the car_list module. You sure it isn't a simple mistake like that?

Think i did, but here it is if you wanna check.

import Tkinter as tk
from PIL import ImageTk, Image



root = tk.Tk()

# create a list of car instances

car_list = []

# first car ...
title = "Golf Mk1 (A1/Typ 17, 1974-1984)"
info = """\
In May, 1974 Volkswagen presented the first-generation
Golf as a modern front wheel drive long-range replacement
of the Beetle.
"""
image = ImageTk.PhotoImage(file='Golf_Mk1.jpg')
# create the class instance and append to the list
car_list.append(Car(title, info, image))

# next car ...
title = "Golf Mk2 (A2/Typ 19E, 1985-1992)"
info = """\
Mk2 that slightly grew in terms of wheelbase, exterior
and interior dimensions while retaining in somewhat more
rounded form the Mk1's overall look. In 1985, the first
Golfs with four-wheel drive (Golf Country) went on sale.
"""
image = ImageTk.PhotoImage(file='Golf_Mk2.jpg')
# create the class instance and append to the list
car_list.append(Car(title, info, image))

# next car ...
title = "Golf Mk3 test"
info = """\
test
"""
image = ImageTk.PhotoImage(file='Golf_Mk3.jpg')
# create the class instance and append to the list
car_list.append(Car(title, info, image))

# and so on ...

By the way i like your Hunter S Thompson quote :)

This makes no sense to me though... even having the module name car_list the same as a variable name within it didn't cause errors when I ran a quick test on importing that.

However, one issue is that your Car class is located in your main file, yet you make calls to it from within your car_list module. That throws me an error. Try re-locating the Car class declaration to the car_list script, and then see if this other weird error is still occurring for you.

EDIT:
And yes, he was a very interesting guy :P

EDIT2:
Moving the Car class to the right module fixed it for me and the script ran the Tk window without an error.

Tried, still get the same error. Could you explain more in depth?

I had no issues running your code... all I did was move the segment:

class Car(object):
    """this class creates a record structure"""
    def __init__(self, title, info, image):
        self.title = title
        self.info = info
        self.image = image

which was in the main file, INTO the module car_list that you had created. This is because you were doing things like car_list.append(Car(title, info, image)) inside the car_list module, yet you had defined the Car class in the main file. I.e. calling an instance of Car in the car_list module was throwing me a "class Car is undefined" error.

That's really strange because it is axactly what i did and still get the error:

in next
print car_num, len (car_list)
NameError: global name 'car_list' is not defined
I'm bit confused now...

What Python installation are you using? I'm running 2.5.4 and after I moved the Car class into the car_list module, your code ran for me without an error... does Python 2.6 disallow the use of main-scope variables sharing the same name as the module or something?

EDIT:
Tested that name-thing on my 2.6 installation and that's not the case. I really have no clue now what is going wrong for you...

Im using 2.6 too...:(

Could you post the code like you tested it? It just seems really weird.

Ok, I'm attaching it, but it'll perform your Tk thing but without the photos - I just commented out the lines in car_list which made the ImageTk.PhotoImage instances as I didn't have the JPGs it was trying to load. I just had it pass stuff like "Golf1" and "Golf2" to the Car class as the image variable instead. Try this code and I hope it works...

Yes, it works fine without the pics, however when i unedited that which you edited and added pics in the same folder i got this:

show(car_num)
File "C:\Users\meemee\Documents\DSV\Python\encyclo\main.py", line 80, in show
screen.image_create('2.0', image=car_list[car_num].image)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 2991, in image_create
*self._options(cnf, kw))
TclError: image "pyimage1" doesn't exist


Nb: Even though the code is the same in my test and your test, the errors are different.

Is that because you forgot to delete the lines I added in that changed the "image" variable to strings like "Golf1", "Golf2", etc.? I put those in right under the image lines that I had commented out.

If that's the case, then it's still assigning strings to the image variable of the Car class instances...

No, changed them back and forth to see the differences but as i said, as soon as i to image changed it from your "image"variable to an image, i get an error.

I just thought about it... is it giving strange errors because you initialize a Tk thing in both the main script and the module you're importing? Shouldn't you only have the root = tk.Tk() present in the main script? The module shouldn't have it because you're importing it into the main one, which already has a Tk() class instance...

At least, this is how it works with wxPython, so maybe it's the deal here. (In wxPython, you only make the wx.App() instance in the main file; in the modules you import wx so that you can subclass it and do stuff like you do in your car_list module, but you don't make a new wx.App() in each subsequent module.)

Sorry if the above was confusing :P

I tried it in the main only but then i get a message saying to early "to early to create image" because TK has to be initialized before that. I found a link that takes up the topic but i cant get my head around it. http://mail.python.org/pipermail/python-list/2004-April/258621.html.
Im open for new suggestions (a fresh take), like maybe appending it differently. could also try opening from a text file but not sure how rhat works either.

Instead of appending Tk image instances to the list, try passing the image filenames in the car_list module to the list. Then when you use them in the main script, just create the Tk image instance then. Maybe that's a better solution...

Cheers Shadwickman, you are a positive influence :) but unfortunately i lost you on the passing part "try passing the image filenames in the car_list module to the list"(i do not understand)

PS. Im not a genius, just tryin to expand my mind

I think he means instead of adding it on to a list(append). just make an instance of the image filename in the car_list module.

Not quite Clueless :P

Haha I just meant this:
You currently (for each car) make the image variable for the Car instance a Tk image thing. Then you append this Car instance onto the car_list variable within the car_list module.

What I suggested is:
Make the image variable for each Car instance in that list just the filename to that car's image (a string). Then, in your page-displaying function within the main script, have it create a Tk image of that filename for that image variable before displaying it. That way all the Tk instances are created from within the main script and your car_list module doesn't need to use the Tk stuff in it.

Ahh, I see it now too.. Oh, thats why I have the name 'Clueless' lol =)
I learn from other threads too..or try.

Lead by example....:) o yeah.... ehm .....cluelesss keep being that

Jokes... Sir Clueless...

So did you try what it was that I was suggesting then? It seemed logical and I think it would work.

I can take a joke =) and I can also serve them :P

i'm on it....:)
As i said,no nobel prize here

OOOOOhhhhh, Clueless86 has no clue............

Just a joke sir
thank you for getting invovled in our discussion....:)

I hate to spam this thread more than it already has had, but people: PLEASE stop posting when you can still edit your last post. It just clutters things and makes this thread annoyingly unmanageable. This last page of posts was useless stuff that didn't deserve new posts for anyways.

My bad.... shadwickman, yes i tried a few things but not quite getting the response i want.I might be trying the functions(not sure),however still no positive feedback from my editor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.