【发布时间】:2021-11-16 22:19:10
【问题描述】:
我有一个带有条目和 4 个按钮的 CRUD 表单,用于从我的数据库中删除、更新、创建、获取值,我想实现另一个按钮来打开一个绑定到我的 的图像>id entry 也可以与我的删除、更新、创建、获取按钮一起使用,我一直在尝试使用 BLOB,并且可以将图像保存在我的数据库中斑点。实际上我知道我需要为我的条目创建 textvariables 像 'idvar = StringVar() , namevar = Stringvar()..., 等等',所以我不知道该怎么做一个图像标签,以便使用我的 CRUD 按钮删除、更新、创建、获取
这是我目前得到的代码,它可以很好地将图像保存到我的照片列中:
from tkinter import *
import sqlite3
top = Tk()
top.configure(width='444', heigh='400')
conn = sqlite3.connect('test.db')
c = conn.cursor()
def enterdata():
id = 'hello'
photo = convert_pic()
c.execute('INSERT INTO test (id, photo) VALUES (?, ?)', (id, photo)) #Here my id has integer value and my photo has BLOB value in my database
conn.commit()
def convert_pic():
filename = 'images/image6.jpg'
with open(filename, 'rb') as file:
photo = file.read()
return photo
btn = Button(top, text='save photo', command=enterdata)
btn.place(x='100', y='111')
mainloop()
【问题讨论】:
-
注意:
command=enterdata()应该是command=enterdata。 -
@CoolCloud 我已经编辑过了。
-
图片是否正确保存在数据库中?
-
@CoolCloud 是的,当我按下按钮
btn = Button(top, text='save photo', command=enterdata时,图像'images/image6.jpg'已正确保存在我的数据库中
标签: python python-3.x tkinter blob tkinter-entry