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.