用python的tkinte库实现一个可视化超市购物清单系统

要实现一个可视化超市购物清单系统,你可以使用Python的Tkinter库来创建用户界面,并使用之前定义的购物清单功能来管理清单数据。下面是一个示例代码,演示如何使用Tkinter和购物清单功能创建一个简单的可视化超市购物清单系统:

import tkinter as tk  
from tkinter import messagebox  
  
# 创建购物清单字典  
shopping_list = {}  
  
# 添加商品到购物清单  
def add_item():  
    item = entry_item.get().strip()  
    quantity = entry_quantity.get().strip()  
    if item and quantity:  
        shopping_list[item] = int(quantity)  
        messagebox.showinfo("Added Item", f"Item '{item}' added to the shopping list.")  
        clear_entries()  
  
# 移除商品从购物清单  
def remove_item():  
    item = entry_item.get().strip()  
    quantity = entry_quantity.get().strip()  
    if item and quantity:  
        if int(quantity) >= shopping_list[item]:  
            del shopping_list[item]  
            messagebox.showinfo("Removed Item", f"Item '{item}' removed from the shopping list.")  
        else:  
            messagebox.showerror("Error", f"Insufficient quantity for item '{item}'.")  
        clear_entries()  
  
# 清除输入框内容  
def clear_entries():  
    entry_item.delete(0, tk.END)  
    entry_quantity.delete(0, tk.END)  
  
# 显示购物清单  
def show_list():  
    if shopping_list:  
        for item, quantity in shopping_list.items():  
            listbox.insert(tk.END, f"{item}: {quantity}")  
    else:  
        listbox.insert(tk.END, "No items in the shopping list.")  
  
# 创建主窗口和控件  
root = tk.Tk()  
root.title("Supermarket Shopping List")  
  
# 添加商品名称和数量的输入框  
label_item = tk.Label(root, text="Item:")  
label_quantity = tk.Label(root, text="Quantity:")  
entry_item = tk.Entry(root)  
entry_quantity = tk.Entry(root)  
button_add = tk.Button(root, text="Add to List", command=add_item)  
button_remove = tk.Button(root, text="Remove from List", command=remove_item)  
button_show = tk.Button(root, text="Show List", command=show_list)  
button_clear = tk.Button(root, text="Clear Entries", command=clear_entries)  
  
# 添加商品到购物清单的布局(垂直排列)  
add_frame = tk.Frame(root)  
add_frame.pack()  
label_item.grid(row=0, column=0, padx=10, pady=10)  
entry_item.grid(row=0, column=1, padx=10)  
label_quantity.grid(row=1, column=0, padx=10, pady=10)  
entry_quantity.grid(row=1, column=1, padx=10)  
button_add.grid(row=2, column=1, padx=10)  
button_remove.grid(row=3, column=1, padx=10)  
button_clear.grid(row=4, column=1, padx=10)  
  
# 显示购物清单的布局(水平排列)
list_frame = tk.Frame(root)
list_frame.pack()
listbox = tk.Listbox(list_frame, width=50, height=15)
listbox.pack(side=tk.LEFT)
button_close = tk.Button(list_frame, text="Close", command=root.destroy)
button_close.pack(side=tk.RIGHT)

# 调整窗口大小和位置
root.geometry("400x300+500+100")

# 进入主循环,显示窗口
root.mainloop()

这个示例代码使用Tkinter库创建了一个简单的可视化超市购物清单系统。用户可以通过输入商品名称和数量,然后添加到购物清单中。也可以从购物清单中移除商品。最后,可以通过点击“显示列表”按钮来查看购物清单的内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值