【案例】使用tkinter创建物流计算器

使用tkinter创建物流计算器

# coding:utf-8
from tkinter import *
from tkinter import ttk
from tkinter.messagebox import *
from math import ceil

with open("计算规则.txt", "rb") as f:  # 打开文件
    data = f.read()  # 读取文件
exec(data)

def mFunc(event):
    category["values"] = tuple(products[product.get()])
    category.current(0)

def check():
    product_value = product.get()
    category_value = category.get()
    quantity_value = quantity.get()
    city_value = city.get()

    weight = products[product_value][category_value] * int(quantity_value)

    d = dict()
    for key,value in free[cities[city_value]].items():
        d.update({key:[value[0],value[1](weight)]})
    d = sorted(d.items(),key = lambda x:x[1][1])


    monty2_1 = ttk.LabelFrame(win, text='结算结果')
    monty2_1.grid(column=3, row=7,sticky='EW', padx=4, pady=4)
    tree = ttk.Treeview(monty2_1,columns=['1','2','3'],show='headings')
    tree.column('1',width=100,anchor='center')
    tree.column('2',width=100,anchor='center')
    tree.column('3',width=100,anchor='center')
    tree.heading('1',text='仓库')
    tree.heading('2',text='物流')
    tree.heading('3',text='运费')
    for i in d:
        tree.insert('','end',values= [i[1][0],i[0],round(i[1][1],2)])
    tree.grid()

win = Tk()
win.title("物流费用计算器")
width,height = 500,500
screenwidth = win.winfo_screenwidth()  
screenheight = win.winfo_screenheight() 
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
win.geometry(alignstr)


'''下拉框样式'''
product_label = Label(win, text='请选择商品:')
product = ttk.Combobox(win)
product['value'] = tuple(products)
product.current(0)
product_value = product.get()



category_label = Label(win, text='请选择规格:')
category = ttk.Combobox(win)
category['value'] = tuple(products[product_value])
category.current(0)
category_value = category.get()

quantity_label = Label(win, text='请选择数量:')
quantity = Entry(win, bd =1)
quantity_value = quantity.get()

city_label = Label(win, text='请选择城市:')
city = ttk.Combobox(win)
city['value'] = tuple(cities)
city.current(0)
city_value = city.get()

monty2_1 = ttk.LabelFrame(win, text='计算结果')
monty2_1.grid(column=3, row=7,sticky='EW', padx=4, pady=10)
tree = ttk.Treeview(monty2_1,columns=['1','2','3'],show='headings')
tree.column('1',width=100,anchor='center')
tree.column('2',width=100,anchor='center')
tree.column('3',width=100,anchor='center')
tree.heading('1',text='仓库')
tree.heading('2',text='物流')
tree.heading('3',text='运费')
tree.grid()


product_label.grid(row=2, column=1, sticky='EW', padx=4,pady=10)
product.grid(row=2, column=3, sticky='EW', padx=4,pady=10)

category_label.grid(row=3, column=1, sticky='EW', padx=4,pady=10)
category.grid(row=3, column=3, sticky='EW', padx=4,pady=10)

quantity_label.grid(row=4, column=1, sticky='EW', padx=4,pady=10)
quantity.grid(row=4, column=3, sticky='EW', padx=4,pady=10)

city_label.grid(row=5, column=1, sticky='EW', padx=4,pady=10)
city.grid(row=5, column=3, sticky='EW', padx=4,pady=10)

    
check = Button(win, text='开始计算', command=check)
check.grid(row=6,column = 3,stick='EW',  padx=4,pady=10)

product.bind("<<ComboboxSelected>>",mFunc)

win.mainloop()

【计算规则.txt】文件内容如下

products = {
    "川宁红茶(黄/红)":{"400包/件":1},
    "川宁铁盒红/蓝":{"200份/件":1},
    "锤纹杯":{"400包/件":16},
    "费列罗T2":{"96*4/件":6.6},
    "费列罗T3":{"96*3/件":4},
}

cities = {
    '杭州':'一区',
    '宁波':'一区',
    '温州':'一区',
    '台州':'一区',
    '上海':'一区',
}

free = {
'一区':{
        'WL-京东':['三墩仓',lambda x:70+[0,ceil(x-83)*0.84][x>83]],
        'WL-德邦':['三墩仓',lambda x:9.1+[0,ceil(x-3)*1.05][x>3]],
        },
    }
posted @ 2021-05-26 23:39  介个车车烫屁股  阅读(253)  评论(2)    收藏  举报