commit_0531

main
zy52306tg 6 months ago
parent 9d0cd96046
commit fd2e00e507

@ -1,7 +1,7 @@
import tkinter as tk
from login_next import * from login_next import *
import main
def login_1(): def login_1():
global new_window
new_window = tk.Tk() new_window = tk.Tk()
new_window.title("欢迎来到登陆成功后的界面") new_window.title("欢迎来到登陆成功后的界面")
new_window.geometry("300x200") new_window.geometry("300x200")
@ -14,12 +14,14 @@ def login_1():
root.pack() root.pack()
# 创建按钮 # 创建按钮
view_image_button = tk.Button(root, text="查看图片信息", command=view_all_images_info)
upload_image_button = tk.Button(root, text="上传图片", command=upload_image) upload_image_button = tk.Button(root, text="上传图片", command=upload_image)
download_button = tk.Button(root, text="下载图片", command=download_image) download_button = tk.Button(root, text="下载图片", command=download_image)
delete_button = tk.Button(root, text="删除图片", command=delete_image) delete_button = tk.Button(root, text="删除图片", command=delete_image)
exit_button = tk.Button(root, text="返回", command=new_window.destroy) exit_button = tk.Button(root, text="返回", command=login_return_main)
# 布局 # 布局
view_image_button.pack()
upload_image_button.pack() upload_image_button.pack()
download_button.pack() download_button.pack()
delete_button.pack() delete_button.pack()
@ -27,6 +29,9 @@ def login_1():
new_window.mainloop() new_window.mainloop()
def login_return_main():
new_window.destroy()
main.main()
if __name__ == "__main__": if __name__ == "__main__":
login_1() login_1()

@ -1,6 +1,6 @@
import pymysql.cursors import pymysql.cursors
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog,messagebox
import os import os
# 数据库连接参数 # 数据库连接参数
@ -45,42 +45,37 @@ def upload_image():
sql = "INSERT INTO images (filename, content) VALUES (%s, %s)" sql = "INSERT INTO images (filename, content) VALUES (%s, %s)"
cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content))) cursor.execute(sql, (os.path.basename(selected_image_path), pymysql.Binary(image_content)))
connection.commit() connection.commit()
print(f"Image '{selected_image_path}' uploaded successfully.") messagebox.showinfo("上传成功", f"图片路径: '{selected_image_path}' 上传成功.")
except Exception as e:
messagebox.showerror("上传错误", f"图片上传失败: {e}")
finally: finally:
connection.close() connection.close()
def delete_image(): def delete_image():
view_all_images_info()
# 获取用户输入的图像ID # 获取用户输入的图像ID
image_id = tk.simpledialog.askinteger("删除图片", "请输入需要删除的图像ID:") image_id = tk.simpledialog.askinteger("删除图片", "请输入需要删除的图像ID:")
if image_id is not None: if image_id is not None:
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME) connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
try: try:
with connection.cursor() as cursor: with connection.cursor() as cursor:
# 查询图像信息 # 直接执行删除操作
cursor.execute("SELECT * FROM images WHERE id = %s", (image_id,)) cursor.execute("DELETE FROM images WHERE id = %s", (image_id,))
image_data = cursor.fetchone() connection.commit()
if image_data:
# 输出图像信息
print(f"Image Details:")
for index, value in enumerate(image_data):
column_name = cursor.description[index][0]
print(f"{column_name}: {value}")
# 提示用户确认删除 # 提示用户确认删除
confirmation = tk.messagebox.askyesno("Confirm Delete", f"是否删除图片ID为 {image_id}的图片?") confirmation = tk.messagebox.askyesno("Confirm Delete", f"是否删除图片ID为 {image_id}的图片?")
if confirmation: if confirmation:
# 执行删除操作 messagebox.showinfo("删除成功", f"成功删除图片ID为 {image_id} 的图片.")
sql = "DELETE FROM images WHERE id = %s"
cursor.execute(sql, (image_id,))
connection.commit()
print(f"成功删除图片ID为 {image_id} 的图片.")
else: else:
print(f"没有发现图片ID为 {image_id}的图片.") messagebox.showinfo("操作取消", "图片删除操作已取消。")
except Exception as e: except Exception as e:
print(f"Error deleting image: {e}") messagebox.showerror("删除错误", f"图片删除失败: {e}")
finally: finally:
connection.close() connection.close()
def download_image(): def download_image():
view_all_images_info()
# 获取用户输入的图像ID # 获取用户输入的图像ID
image_id = tk.simpledialog.askinteger("下载图片", "请输入需要下载的图像ID:") image_id = tk.simpledialog.askinteger("下载图片", "请输入需要下载的图像ID:")
if image_id is not None: if image_id is not None:
@ -100,18 +95,37 @@ def download_image():
try: try:
with open(save_path, 'wb') as image_file: with open(save_path, 'wb') as image_file:
image_file.write(content) image_file.write(content)
print(f"Image downloaded successfully to '{save_path}'.") messagebox.showinfo("成功", f"图片已成功下载到 '{save_path}'")
except IOError as e: except IOError as e:
print(f"Error writing to file: {e}") messagebox.showerror("错误", f"写入文件时出错: {e}")
else: else:
print("Download cancelled.") messagebox.showinfo("取消", "下载已取消。")
else: else:
print(f"No image found with ID {image_id}.") messagebox.showinfo("未找到", f"未找到ID为 {image_id} 的图片。")
except Exception as e: except Exception as e:
print(f"Error downloading image: {e}") messagebox.showerror("下载错误", f"下载图片时发生错误: {e}")
finally: finally:
connection.close() connection.close()
def view_all_images_info():
connection = pymysql.connect(host=DB_HOST, user=DB_USER, password=DB_PASSWORD, db=DB_NAME)
try:
with connection.cursor() as cursor:
# 查询所有图片信息
cursor.execute("SELECT id, filename FROM images")
image_data = cursor.fetchall()
if image_data:
# 构建信息消息
message = "全部图片信息:\n\n"
for row in image_data:
message += f"Image ID: {row[0]}\nImage Name: {row[1]}\n---\n"
messagebox.showinfo("所有图片信息", message)
else:
messagebox.showinfo("未找到图片", "数据库中没有图片信息。")
except Exception as e:
messagebox.showerror("查询错误", f"Error fetching image info: {e}")
finally:
connection.close()

@ -1,6 +1,5 @@
from register import * from register import *
from login import * from login import *
import os
def main(): def main():
global root global root
@ -46,10 +45,11 @@ def register_jc():
root.destroy() root.destroy()
register() register()
def login_jc(): def login_jc():
root.destroy() root.destroy()
login_1() login_1()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

@ -1,5 +1,6 @@
import tkinter as tk import tkinter as tk
from sql import * from sql import *
import main
def register(): def register():
global register_view global register_view
@ -21,7 +22,7 @@ def register():
register_button = tk.Button(register_view, text="注册", command=register_0) register_button = tk.Button(register_view, text="注册", command=register_0)
register_button.pack() register_button.pack()
back_button = tk.Button(register_view, text="返回", command=register_view.destroy) back_button = tk.Button(register_view, text="返回", command=register_return_main)
back_button.pack() back_button.pack()
register_view.mainloop() register_view.mainloop()
@ -37,5 +38,9 @@ def register_0():
ok_label = tk.Label(register_view, text="注册成功") ok_label = tk.Label(register_view, text="注册成功")
ok_label.pack() ok_label.pack()
def register_return_main():
register_view.destroy()
main.main()
if __name__ == "__main__": if __name__ == "__main__":
register() register()

@ -1,7 +1,5 @@
from flask import * from flask import *
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
import pymysql
import os
app = Flask(__name__) app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/file' app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123456@localhost:3306/file'
@ -24,12 +22,11 @@ def add_user(username, password):
db.session.commit() db.session.commit()
try: try:
with app.app_context(): with app.app_context():
# 尝试连接数据库
db.create_all() db.create_all()
print("数据库连接成功!")
except Exception as e: except Exception as e:
print(f"数据库连接失败: {str(e)}") print(f"数据库连接失败: {str(e)}")

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

Loading…
Cancel
Save