import tkinter as tk from tkinter import messagebox import pymysql conn = pymysql.connect(host='localhost',user='root', password='pananjun', db='cus', charset='utf8') cursor = conn.cursor() class 查找元素(tk.Tk): def __init__(self): super().__init__() self.title('查找元素') self.geometry('600x400') frame = tk.Frame(self) frame.pack(padx=20, pady=20, fill=tk.BOTH, expand=True) self.var = tk.StringVar() entry = tk.Entry(frame, textvariable=self.var) entry.grid(row=0, column=0, padx=10, pady=10) lookup_btn = tk.Button(frame, text='查找', command=self.lookup) lookup_btn.grid(row=0, column=1, padx=10, pady=10) # 显示查找结果 self.result_var = tk.StringVar() result_label = tk.Label(frame, textvariable=self.result_var) result_label.grid(row=1, column=0, columnspan=2, padx=10, pady=10) def lookup(self): element = self.var.get() # 查询SQL语句 sql = f"SELECT * FROM 商品表 WHERE 商品编号 = '{element}'" cursor.execute(sql) result = cursor.fetchone() if result: self.result_var.set(f'该编号已存在禁止入库:{result[0]}') else: self.result_var.set('未找到该编号,允许入库') # 主程序 app = 查找元素() app.mainloop() # 关闭数据库连接 conn.close()