从 sqlite3 检索图像并在 Kivy 图像小部件中显示 - ValueError
要求
我正在尝试从数据库中检索图像并将此图像设置为 kivy 图像小部件,此操作会引发 ValueError,不确定原因.欢迎任何意见.
数据库:Sqlite3
表名:用户
列:用户 ID、用户名、用户图像
def populate_fields(self): # NEW# 代码在此处检索文本数据并显示在文本输入字段中.# 第 1 步:检索图像连接 = sqlite3.connect("demo.db")有连接:游标 = 连接.游标()cursor.execute("从用户那里选择 UserImageUserID=?",self.data_items[columns[0]]['text'])图像 = cursor.fetchone()数据 = io.BytesIO(图像[0])#STEP 2:将输出设置为图像小部件self.image.source = 数据 # --->触发错误
错误追溯:
self.image.source = 数据文件kivyweakproxy.pyx",第 33 行,在 kivy.weakproxy.WeakProxy.__setattr__ (kivyweakproxy.c:1471)文件kivyproperties.pyx",第 478 行,在 kivy.properties.Property.__set__ (kivyproperties.c:5572)文件kivyproperties.pyx",第 513 行,在 kivy.properties.Property.set (kivyproperties.c:6352)文件kivyproperties.pyx",第 504 行,在 kivy.properties.Property.set (kivyproperties.c:6173)文件kivyproperties.pyx",第 676 行,在 kivy.properties.StringProperty.check (kivyproperties.c:8613)ValueError: Image.source 只接受 str
解决方案 io.BytesIO()
执行后,data
为Bytes.使用 Kivy
REQUIREMENT
I'm trying to retrieve an image from Database and set this image to kivy image widget, this operation throws a ValueError, unsure of the cause. Welcome any inputs.
Database: Sqlite3
Table name: Users
Columns: UserID, UserName, UserImage
def populate_fields(self): # NEW
# Code retrieves text data and display in textinput fields here.
# STEP 1: RETRIEVE IMAGE
connection = sqlite3.connect("demo.db")
with connection:
cursor = connection.cursor()
cursor.execute("SELECT UserImage from Users where
UserID=?",self.data_items[columns[0]]['text'] )
image = cursor.fetchone()
data = io.BytesIO(image[0])
#STEP 2: SET OUTPUT TO IMAGE WIDGET
self.image.source = data # ---> triggers an Error
ERROR TRACEBACK:
self.image.source = data
File "kivyweakproxy.pyx", line 33, in kivy.weakproxy.WeakProxy.__setattr__ (kivyweakproxy.c:1471)
File "kivyproperties.pyx", line 478, in kivy.properties.Property.__set__ (kivyproperties.c:5572)
File "kivyproperties.pyx", line 513, in kivy.properties.Property.set (kivyproperties.c:6352)
File "kivyproperties.pyx", line 504, in kivy.properties.Property.set (kivyproperties.c:6173)
File "kivyproperties.pyx", line 676, in kivy.properties.StringProperty.check (kivyproperties.c:8613)
ValueError: Image.source accept only str
解决方案
After execution of io.BytesIO()
, data
is in Bytes. Use Kivy CoreImage and texture
to convert data
.
Replace
self.image.source = data
with:
self.image.texture = CoreImage(data, ext="png").texture
Image source
source
Filename / source of your image.
source is a StringProperty and defaults to None
Output
相关文章