问题
this def should connect to a database, and if the parameters are correct should retrieve a BLOB image, however i confused on how to display that image. currently, it will display all the other information besides the BLOB image that i want to be displayed. i realize that setting it to a stringvar is not correct. can anyone help with some clarity on how to get this to display in Tkinter? if not, is there any alternative solutions?
func = Tkinter.Toplevel()
func.title("blah")
func.geometry('400x400+0+0)
db = MySQLdb.connect(host='xxx.xxx.xxx.xxx', user='xxx',passwd='xxxx',db='xxxxx')
cursor = db.cursor()
FirstName = QE1.get()
LastName = QE2.get()
SSN = QE3.get()
cursor execute ("""SELECT pat_face FROM PATIENT WHERE pat_firstname=%s AND pat_lastname=%s AND pat_id=%s""",(FirstName,LastName,SSN))
PATFACEresults = StringVar()
PATFACEresults.set(cursor.fetchone())
db.close()
PATFACE = Tkinter.Label(func, textvariable=PATFACEresults).grid(row=0,column=1)
回答1:
I think you'll need PIL for this (and possibly StringIO as well).
something like:
from PIL import Image, ImageTk
import cStringIO
...
results = cursor.fetchone()
data = cStringIO.StringIO(results.tostring())
pic = ImageTk.PhotoImage(Image.open(data))
patface = Tkinter.Label(func, image=pic)
patface.grid(row=0, column=1)
You could also try saving the image direct to a file & loading it from there.
more info:
- http://zetcode.com/databases/mysqlpythontutorial/
- http://www.effbot.org/zone/pil-index.htm
- http://www.effbot.org/tkinterbook/
来源:https://stackoverflow.com/questions/8317421/retrieve-and-displaying-blob-images-from-mysql-database-with-tkinter