首先输入你拥有的钱数,然后输入要你买的水果序号和购买的水果的数量,点击结算按钮就可以查看支付的金额数和所剩余的金额
import tkinter as tk from tkinter import ttk window=tk.Tk() window.title("超市") window.geometry("600x600") #实例化一个标签 stort1=tk.Label(window,text='水果很贵超市\n-------------------------------------------',font="Arial",bg="red",fg="yellow").pack() #创建水果列表 f_name=['西瓜','苹果','香蕉','榴莲','橙子','红枣','芒果','哈密瓜'] #创建水果对应列表 f_price_num=[10,20,12,50,13,14,20,9] #创建序号列表 f_list=[1,2,3,4,5,6,7,8] #创建表格 columns=("水果名字","水果价格(美元)","序号") treeview=ttk.Treeview(window,height=10,show="headings",columns=columns) treeview.heading("水果名字",text="水果名字") #显示表头 treeview.heading("水果价格(美元)",text="水果名价格(美元)") treeview.heading("序号",text="序号") treeview.place(x=10,y=80) #设置宽度 treeview.column("水果名字", width=120, anchor='center') treeview.column("水果价格(美元)", width=130, anchor='center') treeview.column("序号", width=130, anchor='center') #插入函数数据 def test(): for i in range(min(len(f_name),len(f_price_num),len(f_list))): treeview.insert('',i,values=(f_name[i],f_price_num[i],f_list[i])) Button1=tk.Button(window,text="查看水果",width=20,height=2,fg='pink',bg='black',command=test) Button1.place(x=150,y=330) h=tk.Label(window,text="你有多少钱",bg='white') h.place(x=400,y=200) h1=tk.Label(window,text="序号",bg='white') h1.place(x=400,y=170) h2=tk.Label(window,text="数量",bg='white') h2.place(x=400,y=220) h=tk.Entry(window) h.place(x=500,y=200,width=100) h1=tk.Entry(window) h1.place(x=500,y=170,width=100) h2=tk.Entry(window) h2.place(x=500,y=220,width=100) stort2=tk.Label(window,text='购物车\n-------------------------------------------',font="Arial",bg="green",fg="yellow").place(x=100,y=400) text=tk.Text(window,bg='gray') text.place(x=10,y=450,width=390,height=300) def settle_accounts(): e1 = int(h.get()) e2 = int(h1.get()) e3 = int(h2.get()) total_price=int(e3*f_price_num[e2-1]) if e1>=total_price: text.insert("end","你要购买的水果-------------"+str(f_name[e2-1])+"\n") text.insert("end","你需要支付的金额------------"+str(total_price)+"\n") text.insert("end","你剩余的金额为--------------"+str(e1-total_price)+"\n") else: text.insert("end", "钱不够") butt=tk.Button(window,text="结算",width=18,height=2,bg="black",fg="orange",command=settle_accounts) butt.place(x=530,y=510) window.mainloop()
运行结果: