在python中基于tkinter模块连接数据库建立的表格,如何把表格中的元素通过存储器存储在字典里,求帮助
这是我做界面的参考代码
#主界面代码
chucun_chongzhi = 0 #存储充值
win_zhu = tk.Tk()
win_zhu.title("商品显示页面")
win_zhu.geometry("700x700+400+20")
# 标题容器
title_frame = tk.Frame(win_zhu, width=690, borderwidth=1, height=50)
title_frame.place(x=5, y=5)
# 切换页面的按钮容器
qie_but_frame = tk.Frame(win_zhu, width=460, borderwidth=1, height=80)
qie_but_frame.place(x=5, y=55)
# 添加到购物车页面容器
gou_frame = tk.Frame(win_zhu, width=225, borderwidth=1, height=640)
gou_frame.place(x=465, y=55)
# 字典存储信息
cunchu_xuhao = {} #序号
cunchu_gouwuche = {} #购物车
cunchu_gouwuzongjia = {} #购物总价
# 定义商品展示的函数
def show_commodity( frame_C, xx, yy, name, jiage, xuhao):
# 缓冲字典
zancunchu_name_jiage = {}
# 去除¥
zancunchu_name_jiage[name] = jiage.strip('¥')
# 把信息添加到存储器
cunchu_xuhao[xuhao] = zancunchu_name_jiage
fr = tk.Frame(frame_C, width=120, borderwidth=1, height=150, bg="#F2F5A9")
fr.place(x=xx, y=yy)
name_lab = tk.Label(fr, text=name, font=("Arial", 17), bg="#F2F5A9")
name_lab.place(x=7, y=95)
jiage_lab = tk.Label(fr, text=jiage, font=("Arial", 14), bg="#F2F5A9")
jiage_lab.place(x=65, y=95)
xuhao_lab = tk.Label(fr, text=xuhao, font=("Arial", 12), bg="#F2F5A9")
xuhao_lab.place(x=50, y=120)
# 设置商品页面
def put_wuping():
# 商品展示页面容器
show_frame_s = tk.Frame(win_zhu, width=460, borderwidth=1, height=560)
show_frame_s.place(x=5, y=135)
show_commodity(show_frame_s, 20, 20, "香蕉", "¥10", "001")
show_commodity(show_frame_s, 165, 20, "苹果", "¥20", "002")
show_commodity(show_frame_s, 312, 20, "梨子", "¥30", "003")
show_commodity(show_frame_s, 20, 200, "樱桃", "¥40", "004")
show_commodity(show_frame_s, 165, 200, "西瓜", "¥50", "005")
show_commodity(show_frame_s, 312, 200, "葡萄", "¥60", "006")
show_commodity(show_frame_s, 20, 390, "桃子", "¥70", "007")
show_commodity(show_frame_s, 165, 390, "橘子", "¥80", "008")
show_commodity(show_frame_s, 312, 390, "荔枝", "¥90", "009")
show_commodity(show_frame_s, 20, 570, "草莓", "¥90", "010")
return show_frame_s
这里面他是把商品信息写在python存储在 cunchu_xuhao[xuhao] 中,而我是把商品信息通过数据库导入的,想问怎么能做到像他一样的格式存储在cunchu_xuhao[xuhao] 中,下面是我的这部分代码
# 主界面代码
chucun_chongzhi = 0
win_zhu = tk.Tk()
win_zhu.title("商品显示页面")
win_zhu.geometry("700x700+400+20")
# 标题容器
title_frame = tk.Frame(win_zhu, width=690, borderwidth=1, height=50)
title_frame.place(x=5, y=5)
# 切换页面的按钮容器
qie_but_frame = tk.Frame(win_zhu, width=460, borderwidth=1, height=80)
qie_but_frame.place(x=5, y=55)
# 添加到购物车页面容器
gou_frame = tk.Frame(win_zhu, width=225, borderwidth=1, height=640)
gou_frame.place(x=465, y=55)
# 字典存储信息
cunchu_xuhao = {} # 序号
cunchu_gouwuche = {} # 购物车
cunchu_gouwuzongjia = {} # 购物总价格
# 设置商品页面
def put_wuping():
# 商品展示页面容器
show_frame_s = tk.Frame(win_zhu, width=460, borderwidth=1, height=560)
show_frame_s.place(x=5, y=135)
# 创建表格
table_head = ('shop name', 'price', 'shop num')
table_main = ttk.Treeview(show_frame_s, height=16, 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=30, y=5)
# 设置文字对齐
table_main.column('shop name', width=150, anchor='center')
table_main.column('price', width=150, anchor='center')
table_main.column('shop num', width=100, anchor='center')
# 清空表格内容
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)
# # 将数据存储在字典中
# for row in results:
# cunchu_xuhao[row[0]] = row[1]
return show_frame_s
如果需要展示完整代码或问题补充的,请各位大能留言
该回答引用chatgpt:
加在for循环里面
cunchu_xuhao[row[2]] = {
'price': row[1]
}
可以使用Python中的pymysql库连接数据库,并通过执行SQL语句从数据库中获取数据。然后可以将获取到的数据以字典的形式存储起来。以下是示例代码:
import pymysql
# 连接数据库
db = pymysql.connect(host='localhost', user='root', password='123456', db='mydb')
# 获取数据
cursor = db.cursor()
sql = "SELECT * FROM products" # 查询语句
cursor.execute(sql)
results = cursor.fetchall() # 获取所有数据
# 将数据存储成字典
data_dict = {}
for row in results:
id = row[0]
name = row[1]
price = row[2]
data_dict[id] = {"name": name, "price": price}
db.close() # 关闭数据库连接
在这段代码中,通过 pymysql.connect() 函数连接数据库,然后使用 execute() 方法执行SQL语句,再使用 fetchall() 方法获取所有数据。接下来将获取到的数据以字典的形式存储在 data_dict 中,字典的 key 值为数据库中每条数据的id,value为一个字典,包括name和price。
如果需要将字典数据展示在 tk.Treeview 中,则可以参考以下代码:
import tkinter as tk
from tkinter import ttk
# 创建 tk.Tk() 对象和 tk.Treeview 对象
window = tk.Tk()
tree = ttk.Treeview(window)
# 将字典数据展示在 tree 中
for id, data in data_dict.items():
name = data['name']
price = data['price']
tree.insert('', 'end', text=id, values=(name, price))
# 绑定滚动条
vsb = ttk.Scrollbar(window, orient="vertical", command=tree.yview)
hsb = ttk.Scrollbar(window, orient="horizontal", command=tree.xview)
tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
# 显示 tree 和滚动条
tree.pack(side="left", fill="both", expand=True)
vsb.pack(side="right", fill="y")
hsb.pack(side="bottom", fill="x")
window.mainloop()
在这段代码中,首先创建 tk.Tk() 对象和 tk.Treeview 对象,然后使用字典数据将 tk.Treeview 对象填充,最后绑定滚动条并显示出来。需要注意的是,由于 tk.Treeview 对象的结构比较特殊,因此需要使用 tree.insert()
方法将数据添加进去,其中 text 为第一列显示的文本,values 为其他列显示的文本。
需要根据自己的实际情况修改以上代码,比如数据库的连接信息、Treeview 表格的列数、列名等。