Hi, could anyone help me updating my pictures and texts. The first picture and text is okay but then the next just gets added in the same textarea instead of "refreshing the page" and adding the next "page" with a pic and text. I also get this error message.

screen.delete(0.0, END)
NameError: global name 'END' is not defined

Another question is would a tupple with the pictures and info about the picstures be better than a dictionary with the pictures, as i have it at the moment?
Thanks in advance.

# -*- coding: cp1252 -*-
#My encyclopedia.
#An interactive encyclopedia which shows
#pictures and information about them. 

import Tkinter, ImageTk, Image

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

        
#Dictionary with pictures for encyclopedia
myImage = {
    "mk1":ImageTk.PhotoImage(Image.open('img/Golf_Mk1.jpg')),
    "mk2":ImageTk.PhotoImage(Image.open('img/Golf_Mk2.jpg'))
}


def main():
    create_widgets()
    root.mainloop()


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

    #Next buttton
    nexst = Tkinter.Button(root, width = 20, text = "Next", fg = "Blue", command = encyclo  )
    nexst.pack(side = Tkinter.LEFT, fill=Tkinter.X)

    #Cancel button
    stop = Tkinter.Button(root, width = 20, text = "Cancel", fg = "Blue", command = root.destroy)
    stop.pack(side = Tkinter.RIGHT, expand=Tkinter.YES)

def encyclo():
    """ Where pictures and text about them get updated """
    
    #Create picture heading
    screen.insert(0.0, '\n\n\t Golf Mk1 (A1/Typ 17, 1974-1984)\n\n')
    #Insert picture
    screen.image_create('5.1', image=myImage['mk1'])
    #insert information about the picture
    info.insert(0.0, "In May, 1974 Volkswagen presented the first-generation\n"
                "Golf as a modern front wheel drive long-range\n replacement of the Beetle.")
    #Delete picture and heading
    screen.delete(0.0, END)
    #insert new heading
    screen.insert(0.0, "Golf Mk2 (A2/Typ 19E, 1985-1992)\n\n")
    #Insert new picture
    screen.image_create('1.1', image=myImage['mk2'])
    info.delete(0.0, END)
    info.insert(0.0, "Mk2 that slightly grew in terms of wheelbase, exterior \n"
                "and interior dimensions while retaining in somewhat more \n"
                "rounded form the Mk1's overall look. In 1985, the first \n"
                "Golfs with four-wheel drive (Golf Country) went on sale.") 
main()

Recommended Answers

All 9 Replies

Use either Tkinter.END or 'end'

Tkinter.END did not really help with updating the text area.

Does anyone have more ideas?

One major flaw in your program is in function encyclo(). There you put up the picture and info for mk1, but immediately delete it to put up the picture and info of mk2.

Actually an interesting project that combines images and text information. I would use a record like structure easily implemented by a class ...

# -*- coding: cp1252 -*-
# My encyclopedia.
# An interactive encyclopedia which shows
# pictures and information about them.

import Tkinter as tk
from PIL import ImageTk, Image

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)


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

# 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 ...


main()

I pretty much kept your function and global approach, but all these function could be combined in another class to make things more organized, and you would get rid of those unsightly globals.

Nice solution, thanks for the help Lardmeister and vegaseat. I was also thinking since the encyclopedia could end up with a lot of information (and a very long code), could i not add car_list[] to a .txt file? Any ideas on that or where i can find more info on how to go about it?

A text file would be tough, since you have variable multiline text. You could try to pickle dump car_list in one program and then pickle load it in another.

Hmm, not sure how pickle would handle the image objects. Might have to pass the image filename to the class and create the image object in the class. Got to play with that a little ...

Im not too sure about that but i'll check it out and see what happens...

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.