我正在尝试使用tkinter导出我的所有数据库记录,包括存储的图像,但是有问题。我与数据库的连接很好。在我决定访问数据库行记录之前,我的代码一直运行良好。我通过了光路,发现还好。我还写了一个映像配置代码,可以在我的代码中看到。下面是我的代码:
我遍历了代码和图像的位置路径,发现一切正常。 对于我的数据库表,在Mysql编辑器中,我的照片数据类型为varchar(255)。
在Mysql查询编辑器中插入我的照片记录时,我使用了set photo =(c:\ mycomputername \ pictures \ DB \ Tj.jpg),我也使用了set photo = Tj.jpg,但是当我尝试时两者都仍然无法正常工作用我的Python代码调用它们
import pymysql
import mysql.connector
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk
from PIL import ImageTk, Image
import os
import shutil
import db_config
def on_tab_selected(event):
selected_tab = event.widget.select()
tab_text = event.widget.tab(selected_tab, "text")
if tab_text == "All Records":
print("All records selected tab selected")
if tab_text == "Add New Records":
print("Add new records tab selected")
def load_database_results():
global rows
global num_of_rows
try:
con = pymysql.connect(
host = db_config.db_server,
user = db_config.db_user,
password = db_config.db_pw,
database = db_config.db
)
sql = "SELECT * FROM participants"
cursor = con.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
num_of_rows = cursor.rowcount
cursor.close()
con.close()
has_loaded_successfully = True
messagebox.showinfo("Connected to Database", "Connection okay")
except pymysql.InternalError as e:
messagebox.showinfo("Connection Error", e)
except pymysql.OperationalError as e:
has_loaded_successfully = database_error()
except pymysql.ProgrammingError as e:
has_loaded_successfully = database_error()
except pymysql.DataError as e:
has_loaded_successfully = database_error()
except pymysql.IntegrityError as e:
has_loaded_successfully = database_error()
except pymysql.NotSupportedError as e:
has_loaded_successfully = database_error()
return has_loaded_successfully
def database_error(err):
messagebox.showinfo("Error", err)
return False
def image_path(file_path):
open_image = Image.open(file_path)
image = ImageTk.PhotoImage(open_image)
return image
def load_photo_tab1(file_path):
image = image_path(file_path)
imgLabelTab1.configure(image = image)
imgLabelTab1.image = image
filename = "default.jpg"
path = db_config.PHOTO_DIRECTORY + filename
rows = "None"
num_of_rows = "None"
form = tk.Tk()
form.title("The App")
form.geometry("500x280")
tab_parent = ttk.Notebook(form)
tab1 = ttk.Frame(tab_parent)
tab2 = ttk.Frame(tab_parent)
tab_parent.bind("<<NotebookTabChanged>>", on_tab_selected)
tab_parent.add(tab1, text = "All Records")
tab_parent.add(tab2, text = "Add New Records")
fName = tk.StringVar()
sur = tk.StringVar()
dept = tk.StringVar()
fNameTab2 = tk.StringVar()
surTab2 = tk.StringVar()
deptTab2 = tk.StringVar()
#TAB1的小配件
firstLabelTab1 = tk.Label(tab1, text = "FirstName")
surnameLabelTab1 = tk.Label(tab1, text = "Surname")
deptLabelTab1 = tk.Label(tab1, text = "Department")
firstEntryTab1 = tk.Entry(tab1, textvariable = fName)
surnameEntryTab1 = tk.Entry(tab1, textvariable = sur)
deptEntryTab1 = tk.Entry(tab1, textvariable = dept)
#openImageTab1 = Image.open(path)
#imgTab1 = ImageTk.PhotoImage(openImageTab1)
#replacing these three lines with the line next to them below
#imgLabelTab1 = tk.Label(tab1, image = imgTab1) #replacing this line
with the line just below it.
imgTab1 = image_path(path)
imgLabelTab1 = tk.Label(tab1, image = imgTab1)
btnFwd = tk.Button(tab1, text = "Forward")
btnBkw = tk.Button(tab1, text = "Backward")
#WIDGETS TO GRID ON TAB1
firstLabelTab1.grid(row = 0, column = 0, padx = 15, pady = 15)
firstEntryTab1.grid(row = 0, column = 1, padx = 15, pady = 15)
surnameLabelTab1.grid(row = 1, column = 0, padx = 15, pady = 15)
surnameEntryTab1.grid(row = 1, column = 1, padx = 15, pady = 15)
deptLabelTab1.grid(row = 2, column = 0, padx = 15, pady = 15)
deptEntryTab1.grid(row = 2, column = 1, padx = 15, pady = 15)
imgLabelTab1.grid(row=1, column=5, rowspan=3, padx=15, pady=15)
btnBkw.grid(row = 3, column = 0, padx = 15, pady = 15)
btnFwd.grid(row = 3, column = 2, padx = 15, pady = 15)
#WIDGETS FOR TAB2
firstLabelTab2 = tk.Label(tab2, text = "FirstName")
surnameLabelTab2 = tk.Label(tab2, text = "Surname")
deptLabelTab2 = tk.Label(tab2, text = "Department")
firstEntryTab2 = tk.Entry(tab2, textvariable = fNameTab2)
surnameEntryTab2 = tk.Entry(tab2, textvariable = surTab2)
deptEntryTab2 = tk.Entry(tab2, textvariable = deptTab2)
#openImageTab2 = Image.open(path)
replacing these three lines with the line just below them
#imgTab2 = ImageTk.PhotoImage(openImageTab2)
#imgLabelTab2 = tk.Label(tab2, image = imgTab2)
imgTab2 = image_path(path)
imgLabelTab2 = tk.Label(tab2, image = imgTab2)
Another file db_config.py:
PHOTO_DIRECTORY = "C:\\Users\\mycomputername\\Pictures\\DB\\"
db_server = "localhost"
db_user = "root"
db_pw = "****"
db = "work"
我期望这样一种情况,当我运行代码时,代替默认图像,数据库行1,列3(即我的照片列)中的图像应显示在该列中,但显示错误,找不到文件