'Trying to trying to select images from a database and insert them into a Treeview
I am trying to select images from a database with other info and insert them into a Treeview, and this Treevire has to be shown in a notebook,
I have tried the following code:
from PIL import ImageTk, Image
from io import BytesIO
from tkinter import filedialog
from tkinter.filedialog import askopenfile
import sqlite3
class Cardealer():
def __init__(self):
self.root = root
# calling dashboad_checks so its showing the treeview on the root notebook
self.dashboard_checks()
def dashboard_checks(self):
self.con = sqlite3.connect('car dealership.db')
self.cursorObj = self.con.cursor()
self.buying_checks_row = self.cursorObj.execute(
'SELECT checkpic, buyername, carmake, checknum, checkvalue, checkdate FROM cars_selling_checksonly')
self.buying_checks_output = self.cursorObj.fetchall()
self.checkslist_buying_img = []
for record in self.buying_checks_output:
checks_buying_img = ImageTk.PhotoImage(date=record[0])
self.Dashboardcheck_buying_tree.insert("",
END,image=checks_buying_img,values=record[1:])
self.checkslist_buying_img.append(checks_buying_img)
self.con.commit()
def selling_filedialogscheckonepic(self):
global filename, img, images
f_types = [("png", "*.png"), ("jpg", "*.jpg"), ("Allfile", "*.*")]
filename = filedialog.askopenfilename(filetypes=f_types)
img = Image.open(filename)
img = img.resize((700, 270), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
self.selling_checkimagelabel.place(x=20, y=60)
self.selling_check_fob = open(filename, 'rb')
self.selling_check_fob = self.selling_check_fob.read()
self.selling_check_fob = (self.selling_check_fob)
I got the following error:
Traceback (most recent call last):
File "C:\Users\user\Desktop\thecardealer(new version)\main.py", line 3271, in <module>
x = Cardealer()
File "C:\Users\user\Desktop\thecardealer(new version)\main.py", line 70, in __init__
self.dashboard_checks()
File "C:\Users\user\Desktop\thecardealer(new version)\main.py", line 106, in dashboard_checks
checks_buying_img = ImageTk.PhotoImage(date=record[0])
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageTk.py", line 108, in __init__
mode = Image.getmodebase(mode)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 294, in getmodebase
return ImageMode.getmode(mode).basemode
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageMode.py", line 74, in getmode
return _modes[mode]
KeyError: None
Exception ignored in: <function PhotoImage.__del__ at 0x000001FC7DA30E50>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Process finished with exit code 1
I would appreciate any help as I am a new programmer and still learning.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
