客户端2.0写了一大半,搁浅了,主要是懒得写了。(其实是结课了就烂尾了)
主要涉及的就是数据库存储数据和tkinter页面设计,这两个都是要单独去学的,学起来也很快,特别是有这种模板在这,还有每一步,每一行的注释(我注释应该挺好懂的吧,如果有问题留言)
如果要使用数据库还是得先去把数据库装好(数据库就用Mysql就可以),如果不需要使用数据库就可以把关于数据库的部分(标注的部分,get_shuju这个函数下面pass)全部码掉,水果名,价格,序号都手打就可以了。
注释挺细的了就是pymysql,tkinter,不懂的评论区问我。
import pymysql
import tkinter as tk
from tkinter import ttk
# 数据库
# 建立连接
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 = []
# 获取数据
def get_shuju():
# 每次点击‘展示水果‘都会刷新数据,相当于是缓存
cur.execute('select * from shop1;') # shop1 是一个表
lst = cur.fetchall() # 因为上面的cur,拿到的是一个元素为字典的数组
fruit_name.clear()
fruit_price.clear()
for i in lst:
fruit_name.append(i['name'])
fruit_price.append(i['price'])
global xu_hao
xu_hao = [i for i in range(len(fruit_name))]
conn.commit()
# 界面设计
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()
# 你的钱
tk.Label(win, text='你有多钱').place(x=540, y=100)
# 你的钱输出框
Money = tk.Entry(win, bd=5, width=10)
Money.place(x=620, y=97)
# 序号
tk.Label(win, text='序号').place(x=540, y=180)
# 序号输入框
ent_xuhao = tk.Entry(win, bd=5, width=10)
ent_xuhao.place(x=620, y=177)
# 数量
tk.Label(win, text='数量').place(x=540, y=220)
# 数量输入框
ent_quantity = tk.Entry(win, bd=5, width=10)
ent_quantity.place(x=620, y=217)
# 购物车输出框
txt_Text = tk.Text(win, bg='#585858', fg='#F7FE2E', width=63,height=20, bd=8)
txt_Text.place(x=20, y=410)
# 创建表格
table_head = ('fruit name', 'fruit price', 'xu_hao')
table_main = ttk.Treeview(win, height=8, 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')
str_name = '你要购买的水果'
str_price = '你需要支付的金额'
str_rest = '你剩余的金额为'
# 查看水果函数
def table():
get_shuju() # 每次点击‘展示水果‘都会刷新数据
# 清空table_main这个treeview里的数据,不然点击’展示水果‘会有问题
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]))
def main():
def jiesuan():
winNew.destroy()
txt_Text.insert('end', str_name.ljust(20, '-') + str(fruit_name[xuhao]) + '\n')
txt_Text.insert('end', str_price.ljust(19, '-') + str(Total_price) + '(美元)' + '\n')
txt_Text.insert('end', str_rest.ljust(20, '-') + str(money - Total_price) + '(美元)' + '\n')
txt_Text.insert('end', '欢迎下次光临!')
# 拿到输入的水果序号,数量,带的钱数
quantity = int(ent_quantity.get())
xuhao = int(ent_xuhao.get())
money = int(Money.get())
if xuhao < 0 or xuhao >= len(fruit_price):
tk.Label(win, text='输入有误,请重新输入').place(x=540, y=260)
Total_price = quantity*fruit_price[xuhao]
if money < Total_price: # 带的钱不够
tk.Label(win, text='钱不够!指定没有好果汁吃\n请你点击 ''鬼'' 退出程序!!', width=20, height=10, font=('Arial', 30), bg='black', fg='red').place(x=40, y=100)
butt_close = tk.Button(win, text='鬼', font=('Arial', 10),bd=10, width=15, height=3, command=win.destroy) # 直接关闭整个程序
butt_close.place(x=200, y=450)
else:
# 弹扫码支付小窗
winNew = tk.Toplevel(win)
winNew.geometry('320x240')
winNew.title('支付')
lb2 = tk.Label(winNew, text='请出示付款码', width=12, height=2, font=('Arial', 15), fg='green')
lb2.place(relx = 0.3,rely = 0.3)
bt_finish = tk.Button(winNew, text='完成', command=jiesuan) # 在点击完成之后才打印凭条
bt_finish.place(relx=0.7, rely=0.8)
# 展示水果按钮
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=main)
butt_main.place(x=540, y=450)
# 结算标题
tk.Label(win, text='打印凭条\n-----------------------------------', font=('Arial', 15), bg='lightgreen', fg='yellow').place(x=130, y=350)
# 关闭
win.mainloop() # 一定要在所有页面显示的后面
cur.close()
conn.close()
最近又要开始蓝桥备赛了,又会发一些数据结构与算法的文,主要是这些题有的只有C/C++的题解,没学过真的看不懂,自己知道其中的痛苦,淋过雨就要给后人打伞
一直说我文章质量不佳,改了又改,还是不行,也不知道是哪的问题,摆烂了,就这样吧