python tkinter收银台
时间: 2023-09-06 21:11:58 浏览: 168
以下是一个使用Python Tkinter模块编写的简单收银台示例代码:
```python
from tkinter import *
def calculate():
# 获取输入值
price = float(price_entry.get())
quantity = int(quantity_entry.get())
discount = float(discount_entry.get()) / 100
# 计算总价和折扣
total_price = price * quantity
total_discount = total_price * discount
# 计算应付和找零
payable = total_price - total_discount
paid = float(paid_entry.get())
change = paid - payable
# 更新标签
payable_label.config(text=f"应付: {payable}")
change_label.config(text=f"找零: {change}")
# 创建主窗口
root = Tk()
root.title("收银台")
# 添加标签和输入框
Label(root, text="单价:").grid(row=0, column=0)
price_entry = Entry(root)
price_entry.grid(row=0, column=1)
Label(root, text="数量:").grid(row=1, column=0)
quantity_entry = Entry(root)
quantity_entry.grid(row=1, column=1)
Label(root, text="折扣(%):").grid(row=2, column=0)
discount_entry = Entry(root)
discount_entry.grid(row=2, column=1)
Label(root, text="实付:").grid(row=3, column=0)
paid_entry = Entry(root)
paid_entry.grid(row=3, column=1)
# 添加计算按钮
Button(root, text="计算", command=calculate).grid(row=4, column=0)
# 添加结果标签
payable_label = Label(root, text="应付: ")
payable_label.grid(row=5, column=0)
change_label = Label(root, text="找零: ")
change_label.grid(row=6, column=0)
# 运行主循环
root.mainloop()
```
该代码创建了一个简单的收银台GUI界面,用户可以输入单价、数量、折扣和实付金额,单击“计算”按钮后,程序将计算应付和找零,并在窗口中显示结果。
阅读全文