asaad kittaneh
asaad kittaneh

Reputation: 55

How to insert image and values from sqllite3 into tkinter treeview the right way?

I am trying to fetch an image and some values from the sqllite3 database and insert them into the Tkinter Treeview, but I got a problem with the position of the record on the proper columns as well the size of the image, so how to put every value in the right place and resize the image so it can fit the right column.

I'm sorry for the long code, but I'm trying to clarify the issue

example: enter image description here

my code script:

 #load image
def filedialogs(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((365, 270), Image.ANTIALIAS)
    img = ImageTk.PhotoImage(img)

  # added infos,image into db
 def Add_car(self):
       global img, filename
       self.fob = open(filename, 'rb')
      self.fob = self.fob.read()
      entities = (self.makecb.get(),self.modelcb.get(), self.Yearcb.get(),self.lincesplatenum.get(),self.price.get(),self.price.get())

    self.con = sqlite3.connect('car dealership.db')
    self.cursorObj = self.con.cursor()

    self.cursorObj.execute(
        '''INSERT INTO Vechicle_info(carmake, carmodel, caryear,carlincesplatenum, image,price) VALUES(?,?,?,?,?,?)''',
        entities)
    self.con.commit()
    self.cursorObj.close()
    
 def expenses(self):   
       self.carstoselecet_expensetree = ttk.Treeview(self.cartoselectframe,columns=["image", "price", "licenseplate", "year","model", "brand"])
       self.carstoselecet_expensetree.pack()

    self.carstoselecet_expensetree.heading("brand", text="car brand")
    self.carstoselecet_expensetree.heading("model", text="car model")
    self.carstoselecet_expensetree.heading("year", text="car year")
    self.carstoselecet_expensetree.heading("licenseplate", text="car licenseplate")
    self.carstoselecet_expensetree.heading("price", text="car price")
    self.carstoselecet_expensetree.heading("image", text="car image")

    self.carstoselecet_expensetree.column("brand", width=125)
    self.carstoselecet_expensetree.column("model", width=100)
    self.carstoselecet_expensetree.column("year", width=100)
    self.carstoselecet_expensetree.column("licenseplate", width=100)
    self.carstoselecet_expensetree.column("price", width=100)
    self.carstoselecet_expensetree.column("image",width=500)
  
    self.cursorObj = self.con.cursor()
    self.my_row=self.cursorObj.execute('SELECT image, price, carlincesplatenum, caryear, carmodel, carmake FROM Vechicle_info')
    self.cars_expense_output = self.cursorObj.fetchall()
  
    # fetch values from db and insert them into treeview
    self.imglist=[]
    for record in self.cars_expense_output:
       img=ImageTk.PhotoImage(data=record[0])
       self.carstoselecet_expensetree.insert("",END,image=img,values=record[1:])
       self.imglist.append(img)

    self.con.commit()

Upvotes: 0

Views: 395

Answers (1)

acw1668
acw1668

Reputation: 46678

There are few issues:

  • original image (read from file) is used instead of the resized image when inserting into database
  • self.price.get() used twice in entities, first one should be self.fob instead
  • image column should be removed as image is shown in tree column "#0"
  • you need to set the rowheight of the treeview to the height of the image

Below is the modified code to fix the above issues:

#load image
def filedialogs(self):
    f_types = [("png", "*.png"), ("jpg", "*.jpg"), ("Allfile", "*.*")]
    filename = filedialog.askopenfilename(filetypes=f_types)
    img = Image.open(filename)
    img = img.resize((365, 270), Image.ANTIALIAS)
    # save the resized image to self.fob
    with io.BytesIO() as f:
        img.save(f, 'PNG')
        self.fob = f.getvalue()

# added infos,image into db
def Add_car(self):
    # changed first self.price.get() to self.fob
    entities = (self.makecb.get(), self.modelcb.get(), self.Yearcb.get(), self.lincesplatenum.get(), self.fob, self.price.get())

    self.con = sqlite3.connect('car dealership.db')
    self.cursorObj = self.con.cursor()

    self.cursorObj.execute(
        '''INSERT INTO Vechicle_info (carmake, carmodel, caryear, carlincesplatenum, image, price) VALUES (?,?,?,?,?,?)''',
        entities)
    self.con.commit()
    self.cursorObj.close()

def expenses(self):
    # removed image column
    self.carstoselecet_expensetree = ttk.Treeview(self.cartoselectframe,columns=["price", "licenseplate", "year","model", "brand"])
    self.carstoselecet_expensetree.pack()

    # set treeview rowheight option
    s = ttk.Style()
    s.configure('Treeview', rowheight=270)

    self.carstoselecet_expensetree.heading("brand", text="car brand")
    self.carstoselecet_expensetree.heading("model", text="car model")
    self.carstoselecet_expensetree.heading("year", text="car year")
    self.carstoselecet_expensetree.heading("licenseplate", text="car licenseplate")
    self.carstoselecet_expensetree.heading("price", text="car price")
    self.carstoselecet_expensetree.heading("#0", text="car image") # changed "image" to "#0" (tree column)

    self.carstoselecet_expensetree.column("brand", width=125)
    self.carstoselecet_expensetree.column("model", width=100)
    self.carstoselecet_expensetree.column("year", width=100)
    self.carstoselecet_expensetree.column("licenseplate", width=100)
    self.carstoselecet_expensetree.column("price", width=100)
    self.carstoselecet_expensetree.column("#0",width=400) # changed "image" to "#0" (tree column)

    self.cursorObj = self.con.cursor()
    self.my_row = self.cursorObj.execute('SELECT image, price, carlincesplatenum, caryear, carmodel, carmake FROM Vechicle_info')
    self.cars_expense_output = self.cursorObj.fetchall()

    # fetch values from db and insert them into treeview
    self.imglist=[]
    for record in self.cars_expense_output:
        img=ImageTk.PhotoImage(data=record[0])
        self.carstoselecet_expensetree.insert("",END,image=img,values=record[1:])
        self.imglist.append(img)

    self.con.commit()

Upvotes: 2

Related Questions