在框架上跟客户端1.0很像,就是删几个函数,加几个函数。获取数据、查看水果都是一样的;增删改也只是sql语句的变化,界面按键变了点,其他都没变。
import tkinter as tk
from tkinter import ttk
import pymysql
# 建立连接
conn = pymysql.connect(host='localhost',
port=3306,
user='root',
database='db1',
password='123456',
charset='utf8')
# 拿游标
cur = conn.cursor(pymysql.cursors.DictCursor)
fruit_name = []
fruit_price = []
xu_hao = []
def get_shuju():
# 获取数据
cur.execute('select * from shop1;')
lst = cur.fetchall()
fruit_name.clear()
fruit_price.clear()
xu_hao.clear()
for i in lst:
fruit_name.append(i['name'])
fruit_price.append(i['price'])
xu_hao.append(i['id'])
conn.commit()
# 上架就是数据库加一行数据
def add():
# 开小窗
winNew = tk.Toplevel(win)
winNew.geometry('320x240')
winNew.title('上架')
lb2 = tk.Label(winNew, text='品名')
lb2.place(relx=0.2, rely=0.2)
lb2 = tk.Label(winNew, text='价格')
lb2.place(relx=0.2, rely=0.4)
ent_name = tk.Entry(winNew, bd=5, width=10)
ent_name.place(relx=0.3, rely=0.2)
ent_price = tk.Entry(winNew, bd=5, width=10)
ent_price.place(relx=0.3, rely=0.4)
# 数据库操作
def ad():
name = str(ent_name.get())
price = int(float(ent_price.get()))
sql = 'insert into shop1(name,price) values(%s,%s);'
cur.execute(sql,(name,price))
# 点击完成,进行增加数据操作
bt_finish = tk.Button(winNew, text='完成', command=ad)
bt_finish.place(relx=0.7, rely=0.8)
def delete():
winNew = tk.Toplevel(win)
winNew.geometry('320x240')
winNew.title('下架')
lb2 = tk.Label(winNew, text='序号')
lb2.place(relx=0.2, rely=0.2)
ent_id = tk.Entry(winNew, bd=5, width=10)
ent_id.place(relx=0.3, rely=0.2)
# 数据库操作
def shan():
id = int(ent_id.get())
sql = 'delete from shop1 where id=%s ;'
cur.execute(sql,id)
# 关闭
bt_finish = tk.Button(winNew, text='完成', command=shan)
bt_finish.place(relx=0.7, rely=0.8)
def change():
winNew = tk.Toplevel(win)
winNew.geometry('320x240')
winNew.title('调价')
lb2 = tk.Label(winNew, text='序号')
lb2.place(relx=0.2, rely=0.2)
lb2 = tk.Label(winNew, text='价格')
lb2.place(relx=0.2, rely=0.4)
ent_id = tk.Entry(winNew, bd=5, width=10)
ent_id.place(relx=0.3, rely=0.2)
ent_price = tk.Entry(winNew, bd=5, width=10)
ent_price.place(relx=0.3, rely=0.4)
# 数据库操作
def shan():
id = int(ent_id.get())
price = int(ent_price.get())
sql = 'update shop1 set price=%s where id=%s;'
cur.execute(sql,(price,id))
# 关闭
bt_finish = tk.Button(winNew, text='完成', command=shan)
bt_finish.place(relx=0.7, rely=0.8)
# 界面
win = tk.Tk()
win['bg'] = '#BDBDBD'
win.title('超市')
win.geometry('775x750+400+20')
tk.Label(win, text='公平公平还是公平水果超市\n--------------------------------', width=30, fg='orange', bg='#DF3A01', font=('Arial', 20)).pack()
lb1 = tk.Message(win, text='我来鹅城只办三件事', width=300, fg='orange', bg='#DF3A01', font=('Arial', 20))
lb1.place(relx=0.1, rely=0.5, relwidth=0.8, relheight=0.1)
# 创建表格
table_head = ('fruit name', 'fruit price', 'xu_hao')
table_main = ttk.Treeview(win, height=13, show='headings', columns=table_head)
# 设置表头
table_main.heading('fruit name', text='水果名字')
table_main.heading('fruit price', text='水果价格(US刀了)')
table_main.heading('xu_hao', text='序号')
# 设置位置
table_main.place(x=20, y=80)
# 设置文字对齐
table_main.column('fruit name', width=200, anchor='center')
table_main.column('fruit price', width=200, anchor='center')
table_main.column('xu_hao', width=60, anchor='center')
# 查看水果函数
def table():
get_shuju()
x = table_main.get_children()
for item in x:
table_main.delete(item)
for i in range(len(fruit_price)):
table_main.insert('', i, values=(fruit_name[i], fruit_price[i], xu_hao[i]))
# 展示水果按钮
butt_table = tk.Button(win, text='查看水果', height=2, font=("Arial", 12), width=20, bg='black', fg='pink', command=table)
butt_table.place(x=160, y=280)
# 增加按钮
butt_main = tk.Button(win, text='上架', height=2, font=("Arial", 12), width=20, bg='black', fg='orange', command=add)
butt_main.place(x=75, y=500)
# 删除按钮
butt_main = tk.Button(win, text='下架', height=2, font=("Arial", 12), width=20, bg='black', fg='orange', command=delete)
butt_main.place(x=295, y=500)
# 修改按钮
butt_main = tk.Button(win, text='调价', height=2, font=("Arial", 12), width=20, bg='black', fg='orange', command=change)
butt_main.place(x=510, y=500)
# 关闭
btClose = tk.Button(win, text='关闭', command=win.destroy)
btClose.place(relx=0.8, rely=0.9)
win.mainloop()
cur.close()
conn.close()
我注释都写在代码里或者在上一个文章里,结果发的时候说我文章质量较低, 可能的原因为:篇幅太短,广告涉嫌违规,外链过多,缺少代码,图片涉嫌违规。
发上一个的时候我专门把图删了,以为是图的问题,结果可能是我打广告了或者缺少代码吧。