Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从sqlite检索图像并添加到listbox tkinter python_Python 3.x_Sqlite_Tkinter_Blob - Fatal编程技术网

Python 3.x 从sqlite检索图像并添加到listbox tkinter python

Python 3.x 从sqlite检索图像并添加到listbox tkinter python,python-3.x,sqlite,tkinter,blob,Python 3.x,Sqlite,Tkinter,Blob,我试图从我的数据库中获取一个blob图像并将其插入列表框,但我做不到。 有什么办法吗 我的全部代码: from tkinter import * from tkinter import messagebox, Tk, Label, filedialog, font from blobdb import Database from PIL import ImageTk, Image from resizeimage import resizeimage import re import sqlit

我试图从我的数据库中获取一个blob图像并将其插入列表框,但我做不到。 有什么办法吗

我的全部代码:

from tkinter import *
from tkinter import messagebox, Tk, Label, filedialog, font
from blobdb import Database
from PIL import ImageTk, Image
from resizeimage import resizeimage
import re
import sqlite3

def fetch_contact ():
    listBox.delete (0, END)

    sqliteConnection = sqlite3.connect('store.db')
    cursor = sqliteConnection.cursor()
    print("Connected to SQLite")
    sqlite_select_query = """SELECT * from contacts"""
    cursor.execute(sqlite_select_query)
    records = cursor.fetchall()
    for row in records:
        listBox.insert (END, (row[1], row[2]))


def convertToBinaryData(filename):
    # Convert digital data to binary format
    with open(filename, 'rb') as file:
        blobData = file.read()
    return blobData


def insertBLOB(id, name, last_name, photo, phone_num, email, home_address):
    print(id, name, last_name, photo, phone_num, email)
    try:
        sqliteConnection = sqlite3.connect('store.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        sqlite_insert_blob_query = """ INSERT INTO contacts (id, name, last_name, photo, phone_num, email, home_address) VALUES (?, ?, ?, ?, ?, ?, ?)"""

        empPhoto = convertToBinaryData(photo)
        # Convert data into tuple format
        data_tuple = (id, name, last_name, empPhoto,
                      phone_num, email, home_address)
        cursor.execute(sqlite_insert_blob_query, data_tuple)
        sqliteConnection.commit()
        listBox.delete (0, END)
        fetch_contact()

        print("Image and file inserted successfully as a BLOB into a table")
        cursor.close()

    except sqlite3.Error as error:
        print("Failed to insert blob data into sqlite table", error)
    finally:
        if (sqliteConnection):
            sqliteConnection.close()
            print("the sqlite connection is closed")


def clear_contacts():
    global photo, image_entry, name_id, image, show_image, image_label
    photo = 'no_image.jpg'
    image_entry = Entry(app, textvariable=photo)
    name_id = ''
    name_entry.delete(0, END)

    lastname_entry.delete(0, END)

    phone_entry.delete(0, END)

    email_entry.delete(0, END)

    address_entry.delete(0, END)


def send_data():
    global photo, image_entry
    regex_email = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
    regex_names = '^[a-zA-Zا-ی]'
    regex_phone = '^[0-9]'

    def check_email(email):
        if (re.search(regex_email, email)):
            return True
        else:
            return False

    def check_names(name):
        if (re.search(regex_names, name)):
            return True
        else:
            return False

    def check_phone(phone):
        if (re.search(regex_phone, phone)):
            return True
        else:
            return False
    if name_value.get() == '' or lastname_value.get() == '' or phone_value.get() == '' or email_value.get() == '' or address_value.get() == '':
        messagebox.showerror("Required Fields", "Please include all fields")
        return
    if not check_names(name_value.get()):
        messagebox.showerror(
            "Error", "Please Write Your Firstname Corectly(Just a-z A-Z)")
        return
    if not check_names(lastname_value.get()):
        messagebox.showerror(
            "Error", "Please Write Your Lastname Corectly(Just a-z A-Z)")
        return
    if not check_phone(phone_value.get()):
        messagebox.showerror(
            "Error", "Please Write Your Phone Number Corectly(Just 0-9)")
        return
    if not check_email(email_value.get()):
        messagebox.showerror("Error", "This Email Is Not A Valid Email")
        return
    if photo == '':
        photo = 'no_image.jpg'
        image_entry = Entry(app, textvariable=photo)

    name_id = id(name_value.get())

    insertBLOB(name_id, name_value.get(), lastname_value.get(), photo, phone_value.get(), email_value.get(), address_value.get())

def open_image ():
    global photo, image_entry, image, show_image, image_label
    photo = filedialog.askopenfilename (initialdir="/", title='Select A File', filetypes = (('png files', '*.png'), ('jpg files', '*.jpg')))
    image_entry = Entry (app, textvariable=photo)
    image = photo[:]
    show_image = ImageTk.PhotoImage (Image.open (image).resize ((80, 80), Image.ANTIALIAS))
    image_label = Label (app, text='', image=show_image)
    image_label.grid (row = 0, column = 1)


app = Tk()
app.title('Contact List')
app.geometry('400x600')
app.configure(background='RoyalBlue4')
icon_image = ImageTk.PhotoImage(Image.open('Alora_icon.ico').resize((20, 20), Image.ANTIALIAS))
app.iconbitmap(icon_image)

regex_email = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
regex_names = '^[a-zA-Zا-ی]'
regex_phone = '^[0-9]'

# list box
sf= font.Font (family='Helvetica', size=36, weight='bold')
listBox = Listbox (app, height = 6, width = 15, font = sf)
listBox.place (x = 50, y = 280)

# Scrollbar
scrollbar = Scrollbar (app)
scrollbar.place (x = 378, y = 280)

# set scrollbar to list
listBox.configure (yscrollcommand = scrollbar.set)
scrollbar.configure (command = listBox.yview)

fetch_contact()

# name input
name_value = StringVar ()
name = Label (app, text="FIRST NAME")
name.grid (row=1, column=0, sticky=W)
name_entry = Entry (app, textvariable=name_value)
name_entry.grid (row=1, column=1, sticky=W, padx = 4, pady = 2)

# last name input
lastname_value = StringVar ()
lastname = Label (app, text="LAST NAME")
lastname.grid (row=2, column=0, sticky=W)
lastname_entry = Entry (app, textvariable=lastname_value)
lastname_entry.grid (row=2, column=1, sticky=W, padx = 4, pady = 2)

# Image
photo = 'no_image.jpg'
image_entry = Entry (app, textvariable=photo)
image = photo[:]
show_image = ImageTk.PhotoImage (Image.open (image).resize ((80, 80), Image.ANTIALIAS))
image_label = Label (app, text='', image=show_image)
image_label.grid (row = 0, column = 1, padx = 4, pady = 2)

# phone input
phone_value = StringVar ()
phone = Label (app, text="PHONE")
phone.grid (row=3, column=0, sticky=W)
phone_entry = Entry (app, textvariable=phone_value)
phone_entry.grid (row=3, column=1, sticky=W, padx = 4, pady = 2)

# Email input
email_value = StringVar ()
email = Label (app, text="EMAIL")
email.grid (row=4, column=0, sticky=W)
email_entry = Entry (app, textvariable=email_value)
email_entry.grid (row=4, column=1, sticky=W, padx = 4, pady = 2)

# home address input
address_value = StringVar ()
address = Label (app, text="HOME ADDRESS")
address.grid (row=5, column=0, sticky=W)
address_entry = Entry (app, textvariable=address_value)
address_entry.grid (row=5, column=1, sticky=W, padx = 4, pady = 2)

name_id = id (name_value.get())

 
# menubar = Menu (app)
# filemenu = Menu(menubar, tearoff=0)
# filemenu.add_command(label="Add Contact", command= print())
# menubar.add_cascade(label="File", menu=filemenu)
# app.config(menu=menubar)

add_btn = Button (app, text='ADD CONTACTS', width=12, command=lambda: send_data ())
add_btn.grid (row=7, column=1)

add_photo_btn = Button (app, text='ADD PHOTO', width=12, command=lambda: open_image ())
add_photo_btn.grid (row=0, column=0)

# Start program
app.mainloop ()

无法将图像插入
列表框
。但是您可以使用
Text.image\u create()
将图像插入
Text
。然后我可以将其插入列表框吗?不,只能将字符串插入
Listbox
。那么如何将列表框行合并到图像并显示图像?您需要创建一个可滚动的框架,并将图像放在这个框架中,以便可以滚动。请参见,您无法将图像插入
列表框
。但是您可以使用
Text.image\u create()
将图像插入
Text
。然后我可以将其插入列表框吗?不,只能将字符串插入
Listbox
。那么如何将列表框行合并到图像并显示图像?您需要创建一个可滚动的框架,并将图像放在这个框架中,以便可以滚动。看见