943,753 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 654
  • Python RSS
Jul 2nd, 2009
0

Help updating images and texts

Expand Post »
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.

python Syntax (Toggle Plain Text)
  1. # -*- coding: cp1252 -*-
  2. #My encyclopedia.
  3. #An interactive encyclopedia which shows
  4. #pictures and information about them.
  5.  
  6. import Tkinter, ImageTk, Image
  7.  
  8. root = Tkinter.Tk()
  9. root.title("Encyclopedia")
  10.  
  11.  
  12. #Dictionary with pictures for encyclopedia
  13. myImage = {
  14. "mk1":ImageTk.PhotoImage(Image.open('img/Golf_Mk1.jpg')),
  15. "mk2":ImageTk.PhotoImage(Image.open('img/Golf_Mk2.jpg'))
  16. }
  17.  
  18.  
  19. def main():
  20. create_widgets()
  21. root.mainloop()
  22.  
  23.  
  24. def create_widgets():
  25. """ create text area and buttons"""
  26. global screen
  27. global info
  28.  
  29. #Text area for pictures
  30. screen = Tkinter.Text(root, width = 40, height = 13, bg = "Grey")
  31. screen.pack(side = Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.BOTH)
  32. screen.insert(0.0, '\n\n\t Welcome to the Golfs history\n\n')
  33.  
  34. #Text area for information about pictures
  35. info = Tkinter.Text(root, width = 40, height = 8, bg = "White")
  36. info.pack(side = Tkinter.TOP, expand=Tkinter.YES, fill=Tkinter.BOTH)
  37.  
  38. #Previous button
  39. prev = Tkinter.Button(root, width = 20, text = "Previous", fg = "Blue" )
  40. prev.pack(side = Tkinter.LEFT)
  41.  
  42. #Next buttton
  43. nexst = Tkinter.Button(root, width = 20, text = "Next", fg = "Blue", command = encyclo )
  44. nexst.pack(side = Tkinter.LEFT, fill=Tkinter.X)
  45.  
  46. #Cancel button
  47. stop = Tkinter.Button(root, width = 20, text = "Cancel", fg = "Blue", command = root.destroy)
  48. stop.pack(side = Tkinter.RIGHT, expand=Tkinter.YES)
  49.  
  50. def encyclo():
  51. """ Where pictures and text about them get updated """
  52.  
  53. #Create picture heading
  54. screen.insert(0.0, '\n\n\t Golf Mk1 (A1/Typ 17, 1974-1984)\n\n')
  55. #Insert picture
  56. screen.image_create('5.1', image=myImage['mk1'])
  57. #insert information about the picture
  58. info.insert(0.0, "In May, 1974 Volkswagen presented the first-generation\n"
  59. "Golf as a modern front wheel drive long-range\n replacement of the Beetle.")
  60. #Delete picture and heading
  61. screen.delete(0.0, END)
  62. #insert new heading
  63. screen.insert(0.0, "Golf Mk2 (A2/Typ 19E, 1985-1992)\n\n")
  64. #Insert new picture
  65. screen.image_create('1.1', image=myImage['mk2'])
  66. info.delete(0.0, END)
  67. info.insert(0.0, "Mk2 that slightly grew in terms of wheelbase, exterior \n"
  68. "and interior dimensions while retaining in somewhat more \n"
  69. "rounded form the Mk1's overall look. In 1985, the first \n"
  70. "Golfs with four-wheel drive (Golf Country) went on sale.")
  71. main()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jul 2nd, 2009
0

Re: Help updating images and texts

Use either Tkinter.END or 'end'
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Jul 3rd, 2009
0

Re: Help updating images and texts

Tkinter.END did not really help with updating the text area.
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jul 3rd, 2009
0

Re: Help updating images and texts

Does anyone have more ideas?
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jul 3rd, 2009
0

Re: Help updating images and texts

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.
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Jul 3rd, 2009
0

Re: Help updating images and texts

Actually an interesting project that combines images and text information. I would use a record like structure easily implemented by a class ...
python Syntax (Toggle Plain Text)
  1. # -*- coding: cp1252 -*-
  2. # My encyclopedia.
  3. # An interactive encyclopedia which shows
  4. # pictures and information about them.
  5.  
  6. import Tkinter as tk
  7. from PIL import ImageTk, Image
  8.  
  9. class Car(object):
  10. """this class creates a record structure"""
  11. def __init__(self, title, info, image):
  12. self.title = title
  13. self.info = info
  14. self.image = image
  15.  
  16.  
  17. def main():
  18. global car_num
  19.  
  20. car_num = 0
  21. create_widgets()
  22. root.mainloop()
  23.  
  24. def create_widgets():
  25. """ create text area and buttons"""
  26. global screen
  27. global info
  28.  
  29. # text area for pictures
  30. screen = tk.Text(root, width=40, height=13, bg="Grey")
  31. screen.pack(side='top', expand='yes', fill='both')
  32. screen.insert(0.0, '\n\n\t Welcome to the Golfs history\n\n')
  33.  
  34. # text area for information about pictures
  35. info = tk.Text(root, width=40, height=8, bg="White")
  36. info.pack(side='top', expand='yes', fill='both')
  37.  
  38. # previous button
  39. prev = tk.Button(root, width=20, text="Previous", fg="Blue",
  40. command=previous)
  41. prev.pack(side='left')
  42.  
  43. # next buttton
  44. nexst = tk.Button(root, width=20, text="Next", fg="Blue",
  45. command=next)
  46. nexst.pack(side='left', fill='x')
  47.  
  48. # quit button
  49. quit = tk.Button(root, width=20, text="Quit", fg="Blue",
  50. command=root.destroy)
  51. quit.pack(side='right', expand='yes')
  52.  
  53. def next():
  54. global car_num
  55. global car_list
  56.  
  57. print car_num, len(car_list)
  58.  
  59. show(car_num)
  60. # increment to next car number
  61. if car_num < len(car_list)- 1:
  62. car_num += 1
  63.  
  64. def previous():
  65. global car_num
  66.  
  67. # decrement to previous car number
  68. if car_num > 0:
  69. car_num -= 1
  70. show(car_num)
  71.  
  72. def show(car_num):
  73. global car_list
  74. global screen
  75. global info
  76.  
  77. # delete any previous picture, title and info
  78. screen.delete(0.0, 'end')
  79. info.delete(0.0, 'end')
  80. # create picture heading
  81. s = car_list[car_num].title + '\n\n'
  82. screen.insert('1.0', s)
  83. # insert picture
  84. screen.image_create('2.0', image=car_list[car_num].image)
  85. # insert information about the picture
  86. info.insert('1.0', car_list[car_num].info)
  87.  
  88.  
  89. root = tk.Tk()
  90. root.title("Encyclopedia")
  91.  
  92. # create a list of car instances
  93. car_list = []
  94.  
  95. # first car ...
  96. title = "Golf Mk1 (A1/Typ 17, 1974-1984)"
  97. info = """\
  98. In May, 1974 Volkswagen presented the first-generation
  99. Golf as a modern front wheel drive long-range replacement
  100. of the Beetle.
  101. """
  102. image = ImageTk.PhotoImage(file='Golf_Mk1.jpg')
  103. # create the class instance and append to the list
  104. car_list.append(Car(title, info, image))
  105.  
  106. # next car ...
  107. title = "Golf Mk2 (A2/Typ 19E, 1985-1992)"
  108. info = """\
  109. Mk2 that slightly grew in terms of wheelbase, exterior
  110. and interior dimensions while retaining in somewhat more
  111. rounded form the Mk1's overall look. In 1985, the first
  112. Golfs with four-wheel drive (Golf Country) went on sale.
  113. """
  114. image = ImageTk.PhotoImage(file='Golf_Mk2.jpg')
  115. # create the class instance and append to the list
  116. car_list.append(Car(title, info, image))
  117.  
  118. # next car ...
  119. title = "Golf Mk3 test"
  120. info = """\
  121. test
  122. """
  123. image = ImageTk.PhotoImage(file='Golf_Mk3.jpg')
  124. # create the class instance and append to the list
  125. car_list.append(Car(title, info, image))
  126.  
  127. # and so on ...
  128.  
  129.  
  130. 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.
Last edited by vegaseat; Jul 3rd, 2009 at 2:45 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 3rd, 2009
0

Re: Help updating images and texts

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008
Jul 3rd, 2009
0

Re: Help updating images and texts

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 3rd, 2009
0

Re: Help updating images and texts

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 ...
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 4th, 2009
0

Re: Help updating images and texts

Im not too sure about that but i'll check it out and see what happens...
Reputation Points: 10
Solved Threads: 0
Light Poster
freddiecool is offline Offline
37 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Adress book
Next Thread in Python Forum Timeline: Authentication With HTTPS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC