Serializing tkinter objects

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Serializing tkinter objects

willie.lawrence
Hello everybody,
I'm a new user of this List.
OBS: forgive my english (I'm a brazilian student)
My question:
I want save PhotoImage (Tkinter.PhotoImage or/and PIL.tkImage.PhotoImage) objects, but Tkinter classes cannot be serialized, so I thought in get all pixels and serialize this tuple, when in starting of my program post all pixels. The problem is that is a harmful practice to velocity...Look, load a wallpaper of 1600x1200 pixels...

I also try encode the content of file:

# ----------- toEncoder.py -----------
import base64 as b64, shelve
gifFile = open("image.gif", "wb") # loading the gif image
content = gifFile.read()
shelveDB = shelve.open("SimpleDb.db") # where I will save
shelveDB["myGIF"] = b64.b64encode(content)
shelveDB.close()
gifFile.close()
# ----------- EOF -----------

# ----------- reader.py -----------
from Tkinter import *
import shelve
db = shelve.open("SimpleDB.db")
content = db["myGIF"]
top = Tk()
img = PhotoImage()
img.configure(data=content)
testLabel = Label(top, text="test", compound="left", image=img)
testLabel.image = img
testLabel.grid()
db.close()
top.mainloop()
# ----------- EOF -----------
 But, how you can see, I can do it only with GIF Images (using Tkinter.PhotoImage)
Anyway,
any help is welcome!
Reply | Threaded
Open this post in threaded view
|

Re: Serializing tkinter objects

willie.lawrence
Hello every body!
I think that I can serialize the tkinter objects with this way:

A mode is:

# ------- Encoder.py ------------
import base64, shelve
# The idea is encode the data file to persist, and than decode to read...
pngFile = open("image.png", "rb")
content = pngFile.read()
content.close()
db = shelve.open("Db.db")
db["image"] = base64.b64encode(content)
db.close()

# ------- Reader.py ----------
from Tkinter import *
import ImageTk, shelve, base64
db = shelve.open("DB.db")
content = base64.b64decode(db["image"])
db.close()

top = Tk()
_ph = ImageTk.PhotoImage(data=content)
l = Label(top, image=_ph)
l.grid()
top.mainloop()