在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
如果需要展示完整代码或问题补充的,请各位大能留言