首页 > 解决方案 > 如何将存储在 sqlite 中的图像显示到 tkinter 窗口中

问题描述

这是将输入框中的图像添加到数据库中的部分

def add_img():
    db=sq.connect('img.db')
    conn=db.cursor()

    with open(str(data.get()),'rb') as f:
        file=f.read()
    with open('yhu.png','wb') as p:
        p.write(f)
    conn.execute(''' INSERT INTO images(name,data)VALUES(?,?)''',(name,file))

    db.commit()
    db.close()

这部分应该将图像显示到 tkiinter 窗口中

def display():
db=sq.connect('img.db')
conn=db.cursor()
conn.execute('''SELECT * FROM images''')
row=conn.fetchall()

for i in row:

    it=i[1]
    this= open(str(len(i[1])),'wb')
    this.write(base64string.decode('base64'))
    this.close
    imgd=ImageTk.PhotoImage(this)
    panel=tk.Label(image=imgd,height=28,width=30)
    panel.image = imgd
    panel.grid()


db.close()

标签: pythonsqlitetkinter

解决方案


该类PhotoImage可以接受二进制数据作为参数,而无需将其保存到文件中:

imgd = ImageTk.PhotoImage(data=it)

以上假设it是来自原始图像文件的二进制数据。


推荐阅读