如何在tkinter python中将blob显示为表中的图像

如何在tkinter python中将blob显示为表中的图像,python,mysql,tkinter,treeview,Python,Mysql,Tkinter,Treeview,我正在尝试从MySQL数据库中插入和检索图像。我已将图像作为BLOB(longblob)插入。当我能够使用image.show()显示图像时,但我希望图像显示在表格中 为此,我使用了treeview。但我的表格只显示BLOB对象。它没有显示任何图像 这是我的密码 from tkinter import * from PIL import ImageTk,Image from tkinter import filedialog import mysql.connector import io fr

我正在尝试从MySQL数据库中插入和检索图像。我已将图像作为BLOB(longblob)插入。当我能够使用
image.show()
显示图像时,但我希望图像显示在表格中

为此,我使用了treeview。但我的表格只显示BLOB对象。它没有显示任何图像

这是我的密码

from tkinter import *
from PIL import ImageTk,Image
from tkinter import filedialog
import mysql.connector
import io
from tkinter import ttk

root = Tk()

id = Entry(root, width=10, font=("Helvetica", 20), bd=3)
id.pack()

browse_button = Button(root,text ='Browse',command = lambda:open_file())
browse_button.pack()

display_button = Button(root,text ='display',command =lambda:display_file())
display_button.pack()

display_table_button = Button(root,text ='display Table',command =lambda:display_Table())
display_table_button.pack()

def display_Table():

    query = "SELECT * FROM image_db"
    person = mysql.connector.connect(host="localhost", user="root", password="", database="image")
    cursor_variable = person.cursor()
    cursor_variable.execute(query)

    vertical_scrollbar = ttk.Scrollbar(root)
    vertical_scrollbar.pack(side=RIGHT, fill=Y)

    my_tree = ttk.Treeview(root, yscrollcommand= vertical_scrollbar.set)
    my_tree.pack()

    vertical_scrollbar.config(command= my_tree.yview)

    style = ttk.Style(root)
    style.theme_use("winnative")
    style.configure(".", font=("Helvetica", 11))
    style.configure("Treeview.Heading", font=("Helvetica", 11, "bold"))

    my_tree['columns'] = ("id", "data")
    my_tree.column("#0", width=0, stretch='NO')
    my_tree.column("id", width=50, anchor='w')
    my_tree.column("data", width=130, anchor='w')


    my_tree.heading("#0", anchor='w', text='Label')
    my_tree.heading("id", anchor='w', text="Id")
    my_tree.heading("data", anchor='w', text="Image")

    count = 0

    for record in cursor_variable:
        # print(record)
        my_tree.insert(parent='', index='end', iid=count, text='Parent',
                            values=(record[0], record[1]))
        count += 1

    person.close()


def display_file():

    id2 = id.get()
    person = mysql.connector.connect(host="localhost", user="root", password="", database="image")
    cursor_variable = person.cursor()
    sql = "SELECT data FROM image_db WHERE id = '" + id2 + "'"
    cursor_variable.execute(sql)
    all_data = cursor_variable.fetchall()
    image = all_data[0][0]
    image = Image.open(io.BytesIO(image))
    image.show()
    person.commit()
    person.close()


def open_file():
    root.filename = filedialog.askopenfilename(initialdir="/Users/write/PycharmProjects/slider/img", title='Select a File',
                                               filetypes=(('png files', '*.png'), ('jpeg files', '*.jpeg'),
                                                          ('jpg files', '*.jpg')))
    my_label = Label(root, text=root.filename).pack()
    my_image = ImageTk.PhotoImage(Image.open(root.filename))
    path = root.filename
    id1 = id.get()


    person = mysql.connector.connect(host="localhost", user="root", password="", database="image")
    cursor_variable = person.cursor()
    thedata = open(root.filename, 'rb').read()
    sql = "INSERT INTO image_db (id,data) VALUES ('" + id1 + "',%s)"
    cursor_variable.execute(sql, (thedata,))
    person.commit()
    person.close()


root.mainloop()


非常感谢您的帮助。

我已经修改了您的
显示表()

  • 将treeview的
    行高设置为50
  • 将列“#0”的宽度设置为100
  • 调整检索到的图像大小以适合treeview单元格
def display_Table():
...
style=ttk.style(根)
风格、主题和使用(“赢家”)
style.configure(“.”,font=(“Helvetica”,11))
style.configure(“Treeview.Heading”,font=(“Helvetica”,11,“粗体”))
style.configure(“树视图”,行高=50)#设置行高
我的_树['columns']=(“id”,)
my#tree.column(“#0”,width=100,stretch='NO')#设置宽度
my_tree.column(“id”,width=100,anchor='w')
我的树。标题(“#0”,锚='w',文本='Image')
我的树标题(“id”,anchor='w',text=“id”)
计数=0
my_tree.imglist=[]
对于游标_变量中的记录:
img=Image.open(io.BytesIO(记录[1]))
img.缩略图((50,50))#将图像调整到所需大小
img=图像。照片图像(img)
my_tree.insert(parent=”“,index=“end”,iid=count,
image=img,value=(记录[0],)#对图像使用“image”选项
my_tree.imglist.append(img)#保存图像引用
计数+=1
请注意,我使用了一个列表来保存图像引用,以避免垃圾收集

输出:


不能使用
选项显示图像。但是您可以使用
treeview.insert()
中的
image
选项来显示图像。我试图给出image=record[1]选项,但它显示了错误“\u tkinter.TclError:在引号中列出元素,后跟“…N”而不是空格”,我不理解它。
print(record[1])“代码”和“代码”和“代码”是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是什么,它显示的是b,b“\\XC\XC\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\X0\x01\xc7o\xa8d\x00\x00\xff