我想把数据库里的商品表放到我的这个商品预览表里,但代码这块有点不懂,求各位帮忙解惑下
代码部分
import tkinter as tk
from tkinter import ttk
from tkinter import *
#连接数据库
import pymysql
conn = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='mysql',
database='xitong',
charset='utf8')
cursor = conn.cursor()# 创建游标
#管理员添加商品
show_shop=tk.Tk()
show_shop.title("管理员操作页面")
show_shop.geometry("700x700+400+20")
tk.Label(show_shop, text='商品预览表', font=('SimHei', 24)).place(x=270, y=20)
tk.Label(show_shop, text='商品', font=('KaiTi', 15)).place(x=200, y=410)
tk.Label(show_shop, text='价格', font=('KaiTi', 15)).place(x=200, y=460)
tk.Label(show_shop, text='序号', font=('KaiTi', 13)).place(x=200, y=510)
# 商品输入
sname = tk.Entry(show_shop, font=('FangSong', 15), width=12)
sname.place(x=270, y=410)
# 价格输入
price = tk.Entry(show_shop, font=('FangSong', 15), width=12)
price.place(x=270, y=460)
# 序号输入
snum = tk.Entry(show_shop, font=('FangSong', 15), width=12)
snum.place(x=270, y=510)
def Increase():
shop_name = sname.get()
shop_price = price.get()
shop_snum = snum.get()
sql = "insert into shopping (sname, price,snum) values (%s, %s, %s)"
cursor.execute(sql, (shop_name, shop_price, shop_snum))
conn.commit()
def Delete():
shop_name = sname.get()
sql="delete from shopping where sname=%s"
cursor.execute(sql, (shop_name))
conn.commit()
def Revamp():
shop_name = sname.get()
shop_price = price.get()
shop_snum = snum.get()
#update 表名 set 列名1=列值1,列名2=列值2,... where 条件;
sql='update shopping set sname=%s , price=%s where snum=%s ;'
cursor.execute(sql, (shop_name, shop_price,shop_snum))
conn.commit()
# 创建表格
table_head = ('shop name', 'price', 'shop num')
table_main = ttk.Treeview(show_shop, height=13, show='headings', columns=table_head)
# 设置表头
table_main.heading('shop name', text='商品名字')
table_main.heading('price', text='价格')
table_main.heading('shop num', text='序号')
# 设置位置
table_main.place(x=100, y=80)
# 设置文字对齐
table_main.column('shop name', width=200, anchor='center')
table_main.column('price', width=200, anchor='center')
table_main.column('shop num', width=60, anchor='center')
def Select():
sql="select * from shopping"
# 执行SQL查询
cursor.execute(sql)
# 获取查询结果
results=cursor.fetchall()
for i in enumerate(results):
table_main.insert('', i, values=(sname, price, snum))
butt_add_sure = tk.Button(show_shop, text='添加', font=('KaiTi', 15), width=8, command=Increase)
butt_add_sure.place(x=100, y=600)
butt_del_sure = tk.Button(show_shop, text='删除', font=('KaiTi', 15), width=8, command=Delete)
butt_del_sure.place(x=250, y=600)
butt_rev_sure = tk.Button(show_shop, text='修改', font=('KaiTi', 15), width=8, command=Revamp)
butt_rev_sure.place(x=400, y=600)
butt_sel_sure = tk.Button(show_shop, text='查看', font=('KaiTi', 15), width=8, command=Select)
butt_sel_sure.place(x=550, y=600)
show_shop.mainloop()
该回答引用chatgpt:
替换这个方法即可
def Select():
# 清空表格内容
table_main.delete(*table_main.get_children())
# 执行查询语句
sql = "SELECT * FROM shopping"
cursor.execute(sql)
results = cursor.fetchall()
# 将查询结果插入表格
for row in results:
table_main.insert('', 'end', values=row)
您可以使用Python的Tkinter库和sqlalchemy来连接MySQL数据库并显示数据表。
步骤如下:
pip install numpy pandas PyYAML sqlalchemy mysql-connector-python mysql-connector-python-rf
import tkinter as tk
from tkinter import ttk
from sqlalchemy import create_engine
import pandas as pd
# 创建MySQL数据库连接
username = 'your_username'
password = 'your_password'
hostname = 'localhost'
port_number = '3306' # 默认端口号为3306
database_name = 'your_database_name'
db_url = f"mysql+mysqlconnector://{username}:{password}@{hostname}:{port_number}/{database_name}"
# 使用sqlalchemy创建一个引擎
conn = create_engine(db_url)
您需要将您的用户名、密码、主机名、端口号和数据库名替换为正确的值。
# 使用pandas读取MySQL数据表
query = 'SELECT * FROM your_table_name'
df = pd.read_sql(query, conn)
将您的表格名称替换为正确的值。
class DatabaseDisplay(tk.Frame):
# 创建窗口和表格
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
# 设置表格的列名
columns = df.columns.values.tolist()
# 创建表格
self.treeview = ttk.Treeview(self, columns=columns, show="headings")
self.treeview.pack(side="left", fill=tk.BOTH, expand=True)
# 添加表头
for col in columns:
self.treeview.heading(col, text=col.title())
# 添加数据到表格中
for idx, row in df.iterrows():
data = row.tolist()
self.treeview.insert('', 'end', values=data)
# 创建主窗口
root = tk.Tk()
root.title("Database Display")
# 在主窗口上添加DatabaseDisplay
gui = DatabaseDisplay(root)
gui.pack(side="top", fill="both", expand=True)
# 运行应用程序并显示表格
root.mainloop()
将代码插入到您的代码中,并运行应用程序即可在您的Tkinter应用程序中显示MySQL数据表。
如果您的代码有任何问题,请在评论中指出,我会尽力帮助您解决问题。