| | |
Help updating images and texts
![]() |
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.
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.
python Syntax (Toggle Plain Text)
# -*- 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()
Actually an interesting project that combines images and text information. I would use a record like structure easily implemented by a class ...
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.
python Syntax (Toggle Plain Text)
# -*- 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()
Last edited by vegaseat; Jul 3rd, 2009 at 2:45 pm.
May 'the Google' be with you!
![]() |
Similar Threads
- Is Updating Windows Xp Necessary (IT Professionals' Lounge)
- Established Web Site for Sale - MYFUEL.ORG $335 (Websites for Sale)
- Displaying images from mysql database (PHP)
- FF colapses the layout (HTML and CSS)
- update& retrieve images from sql server using vb.net (VB.NET)
- Javascript printing problems (JavaScript / DHTML / AJAX)
- Need help making simple random images (PHP)
- Problem Updating Row in GridView (ASP.NET)
- updating 2 HTML tables on one PHP page (PHP)
Other Threads in the Python Forum
- Previous Thread: Adress book
- Next Thread: Authentication With HTTPS
| Thread Tools | Search this Thread |
address aliased anydbm app bash beginner bits calling casino changecolor cipher clear conversion coordinates corners count cturtle curves definedlines development dictionary digital dynamic events examples excel external feet file float format function gui handling hints homework iframe images import input java keycontrol line linux list lists loan loop matching mouse multiple number numbers output parsing path port prime programming projects py py2exe pygame pymailer python random rational raw_input recursion recursive scrolledtext searchingfile shebang signal singleton split string strings tails terminal text threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable web-scrape whileloop word wxpython xlwt






