wsh_wshkk 2024-07-02 17:33 采纳率: 70%
浏览 6

Python有关的管理系统问题tinker

file_control.py

import tkinter as tk
from tkinter import messagebox
class Product_sales(object):
    def __init__(self):
        self.products_data = []
        self.sales_data = []
        self.load_file_data()

    #插入数据
    def insert_pro(self, data):
        self.products_data.append(data)
        self.save_data()
        print(self.products_data)

    #显示全部数据
    def all_pro(self):
        return self.products_data
    def all_sales(self):
        return self.sales_data


    #删除数据
    def delete_pro(self, category,name):
        flag = 1
        for line in self.products_data:
            if line[0] == category and line[2] == name:
                self.products_data.remove(line)
                messagebox.showinfo('提示', '删除成功')
                flag = 0
                break
            else:
                pass
        if flag == 1:
            messagebox.showinfo('提示', '没有该商品')
        self.save_data()
        print(self.products_data)


    # 查询
    def search_by_infor_pro(self, category,name,manufacturer):
        result=[]
        for data in self.products_data:
            if category == data[0] and name == data[2] and manufacturer == data[7]:
                result.append(data)
        else:
            pass
        return result


    #按照类别查询
    def search_by_category_sales(self, category):
        result=[]
        for data in self.sales_data:
            if category == data[0]:
                result.append(data)
        else:
            pass
        return result

    #按名字查询并处理
    def search_by_name_sales(self,name,number):
        flag = 0
        for line in self.sales_data:
            if line[1] == name:
                line[2] = int(line[2]) - int(number)
                line[4] = int(line[2]) * int(line[3])
                messagebox.showinfo('提示', '退回成功')
                flag = 0
                break
            else:
                flag = 1
        if flag == 1:
            messagebox.showinfo('提示', '没有该商品')
        self.save_data()
        print(self.sales_data)
    def search_by_name_pro(self,name,count):
        # 遍历所有商品,找到用户输入的商品
        product_found = False
        for product in self.products_data:
            if product[2] == name:
                product_found = True
                # 判断库存是否足够
                if count > int(product[5]):
                    messagebox.showerror('错误', '库存不足')
                else:
                    # 更新库存
                    product[5] = str(int(product[5]) - count)
                    flag = 0
                    if self.sales_data==[]:
                        flag = 1
                    else:
                        for line in self.sales_data:
                            if line[1] == name:  # 如果有库存记录
                                line[2] = int(line[2]) + count
                                line[4] = int(line[2]) * int(line[3])
                                flag = 0
                                break
                            else:
                                flag = 1

                    if flag == 1:
                        self.sales_data.append([product[0], name, str(count), product[3],str(int(count) * int(product[3]))])
                    # 提示成功
                    messagebox.showinfo('提示', '销售成功')
                    return

        if not product_found:
            messagebox.showerror('错误', '没有该商品')
        self.save_data()
        print(self.products_data)
        print(self.sales_data)



    # 修改
    def update_pro(self,name,n,content):
        flag = 0
        for product in self.products_data:
            if product[2] == name:
                product[n]=content
                messagebox.showinfo('提示', '修改成功')
                flag = 0
                break
            else:
                flag = 1
        if flag == 1:
            messagebox.showinfo('提示', '没有该商品')
        self.save_data()
        print(self.products_data)


    # 读取文件
    def load_file_data(self):
        with open('products_values', 'r', encoding='utf-8') as file:
            # 读取文件时,保留第一行作为标题
            header = file.readline().strip()
            lines = file.readlines()
        for line in lines:
            product = line.strip().split()
            self.products_data.append(product)

        with open('sales_record', 'r', encoding='utf-8') as file:
            # 读取文件时,保留第一行作为标题
            header = file.readline().strip()
            lines = file.readlines()
        for line in lines:
            data = line.strip().split()
            self.sales_data.append(data)

    # 保存数据
    def save_data(self):
        with open('products_values', 'w', encoding='utf-8') as file:
            file.write('商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商' + ' \n')
            for product_1 in self.products_data:
                line = ' '.join(product_1) + '\n'
                file.write(line)

        with open('sales_record', 'w', encoding='utf-8') as file:
            file.write('商品类别 商品名称 销售数量 单价 总价 ' + ' \n')
            for sales_data in self.sales_data:
                if sales_data != []:
                    line = ' '.join(sales_data) + '\n'
                    file.write(line)

PS=Product_sales()

运行这个主体部分

import tkinter as tk
from tkinter import messagebox
from file_control import Product_sales

class ProductSales:
    def __init__(self):
        self.products = []
        self.sales_data = []

     # 销售记录
    def product_sales(self):
        product_sales_instance=Product_sales()
        product_window = tk.Tk()
        product_window.title('销售记录')
        product_window.geometry('150x200')
        a = tk.Label(product_window, text='   ').grid(row=0, column=0, padx=5, pady=10)

        # 输入类别,按照商品销售数量从小到大排序,显示统计的售出数量和价格
        def statistic():
            sta_window = tk.Tk()
            sta_window.title('按类别统计')
            sta_window.geometry('250x100')
            # 输入类别
            tk.Label(sta_window, text='类别').grid(row=0, column=0)
            category_entry = tk.Entry(sta_window)
            category_entry.grid(row=0, column=1)

            # 添加提交按钮
            def on_submit():
                category = category_entry.get()
                temp=product_sales_instance.search_by_category_sales(category)
                # 显示查询到的结果
                print_window = tk.Tk()
                print_window.title('查询结果')
                print_window.geometry('300x150')
                tk.Label(print_window, text='类别 名称 数量 单价 总价格').grid(row=0, column=0)
                count = 1
                print('1', temp)
                for line in temp:
                    tk.Label(print_window, text=line).grid(row=count, column=0)
                    count += 1
                    print(line)
                # 销毁窗口
                sta_window.destroy()
                print_window.mainloop()

            submit_button = tk.Button(sta_window, text='查询', command=on_submit)
            submit_button.grid(row=1, column=1, padx=10, pady=10)

            # 退出按钮
            def on_exit():
                sta_window.destroy()

            exit_button = tk.Button(sta_window, text='退出', command=on_exit)
            exit_button.grid(row=1, column=0, padx=10, pady=10)
            sta_window.mainloop()

        sta_button = tk.Button(product_window, text='按类别统计', command=statistic)
        sta_button.grid(row=0, column=1, padx=5, pady=10)

        # 显示全部的售出记录
        def all_sales():
            all_window = tk.Tk()
            all_window.title('全部销售记录')
            all_window.geometry('300x150')
            count = 1
            tk.Label(all_window, text='类别 名称 数量 单价 总价格').grid(row=0, column=0)
            sales_data = product_sales_instance.all_sales()
            for line in sales_data:
                tk.Label(all_window, text=line).grid(row=count, column=0)
                count += 1
            # 销毁窗口
            product_window.destroy()
            all_window.mainloop()

        all_button = tk.Button(product_window, text='全部 记录', command=all_sales)
        all_button.grid(row=1, column=1, padx=5, pady=10)

        # 用户退货
        def break_pro():
            break_window = tk.Tk()
            break_window.title('用户退回商品')
            break_window.geometry('300x150')
            # 输入名称
            tk.Label(break_window, text='名称').grid(row=0, column=0)
            name_entry = tk.Entry(break_window)
            name_entry.grid(row=0, column=1)
            # 输入数量
            tk.Label(break_window, text='数量').grid(row=1, column=0)
            number_entry = tk.Entry(break_window)
            number_entry.grid(row=1, column=1)

            # 添加提交按钮
            def on_submit():
                name = name_entry.get()
                number = number_entry.get()
                product_sales_instance.search_by_name_sales(name,number)

            submit_button = tk.Button(break_window, text='确定', command=on_submit)
            submit_button.grid(row=2, column=2, padx=5, pady=10)

            # 退出按钮
            def on_exit():
                break_window.destroy()

            exit_button = tk.Button(break_window, text='退出', command=on_exit)
            exit_button.grid(row=2, column=0, padx=5, pady=10)
            break_window.mainloop()

        b_button = tk.Button(product_window, text='退 回 商 品', command=break_pro)
        b_button.grid(row=2, column=1, padx=5, pady=10)

        # 退出按钮
        def on_exit():
            product_window.destroy()

        exit_button = tk.Button(product_window, text='  退      出  ', command=on_exit)
        exit_button.grid(row=3, column=1, padx=5, pady=10)

        product_window.mainloop()

    #销售功能
    def sales(self):
        product_sales_instance = Product_sales()
        sale_window = tk.Tk()
        sale_window.title('销售窗口')
        sale_window.geometry('300x150')

        # 添加商品名称标签和输入框
        tk.Label(sale_window, text='想购商品名称').grid(row=0, column=0, padx=10, pady=10)
        product_entry = tk.Entry(sale_window)
        product_entry.grid(row=0, column=1, padx=5, pady=5)

        # 添加商品数量标签和输入框
        tk.Label(sale_window, text='想购商品数量').grid(row=1, column=0, padx=10, pady=10)
        count_entry = tk.Entry(sale_window)
        count_entry.grid(row=1, column=1, padx=5, pady=5)


        # 添加提交按钮
        def on_submit():
            product_name = product_entry.get()
            try:
                product_count = count_entry.get()
            except ValueError:    #输入的不是数字
                messagebox.showerror('错误', '请输入有效的数量')
                return

            product_sales_instance.search_by_name_pro(product_name,int(product_count))
            sale_window.destroy()
            self.sales()
        submit_button = tk.Button(sale_window, text='提交', command=on_submit)
        submit_button.grid(row=2, column=1, columnspan=2, padx=10, pady=10)

        # 添加退出按钮
        def on_exit():
            sale_window.destroy()
        exit_button = tk.Button(sale_window, text='退出', command=on_exit)
        exit_button.grid(row=2, column=0, columnspan=2, padx=10, pady=10)
        sale_window.mainloop()

    # 商品管理功能
    def product_Management(self):
        product_sales_instance = Product_sales()
        product_window = tk.Tk()
        product_window.title('商品管理')
        product_window.geometry('300x150')
        # 为了让两个按钮位置居中
        a_text = tk.Label(product_window, text=' ' * 20).grid(row=0, column=0, padx=5, pady=10)

        # 添加功能按钮
        def on_add():
            product_window.destroy()
            add_window = tk.Tk()
            add_window.title('添加商品')
            add_window.geometry('250x400')
            # 添加商品类别
            tk.Label(add_window, text='商品类别').grid(row=0, column=0, padx=10, pady=10)
            category_entry = tk.Entry(add_window)
            category_entry.grid(row=0, column=1, padx=5, pady=5)
            # 添加商品编号
            tk.Label(add_window, text='商品编号').grid(row=1, column=0, padx=10, pady=10)
            id_entry = tk.Entry(add_window)
            id_entry.grid(row=1, column=1, padx=5, pady=5)
            # 添加商品名称
            tk.Label(add_window, text='商品名称').grid(row=2, column=0, padx=10, pady=10)
            name_entry = tk.Entry(add_window)
            name_entry.grid(row=2, column=1, padx=5, pady=5)
            # 添加进货价格
            tk.Label(add_window, text='进货价格').grid(row=3, column=0, padx=10, pady=10)
            price_entry = tk.Entry(add_window)
            price_entry.grid(row=3, column=1, padx=5, pady=5)
            # 添加销售价格
            tk.Label(add_window, text='销售价格').grid(row=4, column=0, padx=10, pady=10)
            sale_price_entry = tk.Entry(add_window)
            sale_price_entry.grid(row=4, column=1, padx=5, pady=5)
            # 添加库存
            tk.Label(add_window, text='库存').grid(row=5, column=0, padx=10, pady=10)
            stock_entry = tk.Entry(add_window)
            stock_entry.grid(row=5, column=1, padx=5, pady=5)
            # 添加供应商名称
            tk.Label(add_window, text='供应商名称').grid(row=6, column=0, padx=10, pady=10)
            supplier_entry = tk.Entry(add_window)
            supplier_entry.grid(row=6, column=1, padx=5, pady=5)
            # 添加生产厂商
            tk.Label(add_window, text='生产厂商').grid(row=7, column=0, padx=10, pady=10)
            manufacturer_entry = tk.Entry(add_window)
            manufacturer_entry.grid(row=7, column=1, padx=5, pady=5)

            # 添加提交按钮
            def on_submit():
                category = category_entry.get()
                id = id_entry.get()
                name = name_entry.get()
                price = price_entry.get()
                sale_price = sale_price_entry.get()
                stock = stock_entry.get()
                supplier = supplier_entry.get()
                manufacturer = manufacturer_entry.get()
                L=[category, id, name, price, sale_price, stock, supplier, manufacturer]
                product_sales_instance.insert_pro(L)
                messagebox.showinfo('提示', '添加成功')
                # 先保存数据,再销毁窗口
                add_window.destroy()
                self.product_Management()

            # 提交按钮,靠右边
            submit_button = tk.Button(add_window, text='提交', command=on_submit)
            submit_button.grid(row=8, column=0, padx=10, pady=10, columnspan=2)

        add_button = tk.Button(product_window, text='添加商品信息', command=on_add)
        add_button.grid(row=0, column=1, padx=5, pady=10)

        # 查询功能
        def on_query():
            product_window.destroy()
            query_window = tk.Tk()
            query_window.title('查询商品信息')
            query_window.geometry('300x150')
            # 输入类别
            tk.Label(query_window, text='类别').grid(row=0, column=0, padx=10, pady=10)
            category_entry = tk.Entry(query_window)
            category_entry.grid(row=0, column=1, padx=5, pady=5)
            # 输入名称
            tk.Label(query_window, text='名称').grid(row=1, column=0, padx=10, pady=10)
            name_entry = tk.Entry(query_window)
            name_entry.grid(row=1, column=1, padx=5, pady=5)
            # 输入生产厂商
            tk.Label(query_window, text='生产厂商').grid(row=2, column=0, padx=10, pady=10)
            manufacturer_entry = tk.Entry(query_window)
            manufacturer_entry.grid(row=2, column=1, padx=5, pady=5)

            # 添加提交按钮
            def on_submit():
                category = category_entry.get()
                name = name_entry.get()
                manufacturer = manufacturer_entry.get()
                temp=product_sales_instance.search_by_infor_pro(category,name,manufacturer)
                if temp==[]:
                    messagebox.showerror('错误', '该记录不存在!')
                else:
                    for line in temp:
                        messagebox.showinfo('提示', '查询成功\n'+line)
                        break
            submit_button = tk.Button(query_window, text='查询', command=on_submit)
            submit_button.grid(row=3, column=0, padx=10, pady=10)

        query_button = tk.Button(product_window, text='查询商品信息', command=on_query)
        query_button.grid(row=1, column=1, padx=5, pady=10)

        # 退出按钮
        def on_exit():
            product_window.destroy()
        exit_button = tk.Button(product_window, text='     退   出    ', command=on_exit)
        exit_button.grid(row=2, column=1, padx=5, pady=10)

        product_window.mainloop()


    #修改功能
    def modify(self):
        product_sales_instance = Product_sales()
        modify_window = tk.Tk()
        modify_window.title('修改商品信息')
        modify_window.geometry('500x300')
        #显示所有商品信息
        tk.Label(modify_window, text='商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商').grid(row=0, column=0)
        count=1
        products=product_sales_instance.all_pro()
        for line in products:
            tk.Label(modify_window, text=line).grid(row=count, column=0)
            count+=1

        def on_exit():
            modify_window.destroy()
        exit_button = tk.Button(modify_window, text='  退  出  ', command=on_exit)
        exit_button.grid(row=10, column=0, padx=5, pady=10, sticky='w')

        #修改记录
        def change():
            change_window = tk.Toplevel(modify_window)
            change_window.title('修改记录')
            change_window.geometry('500x300')
            # 输入名称
            tk.Label(change_window, text='商品名称').grid(row=0, column=0)
            name_entry = tk.Entry(change_window)
            name_entry.grid(row=0, column=1)
            #输入想修改的标题
            tk.Label(change_window, text='1:商品类别 2:商品编号 3:商品名称 4:进货价格').grid(row=1, column=1)
            tk.Label(change_window, text='5:销售价格 6:商品库存 7:供应商名称 8:生产厂商').grid(row=2, column=1)
            tk.Label(change_window, text='想改类题(请输入数字)').grid(row=3, column=0)
            title_entry = tk.Entry(change_window)
            title_entry.grid(row=3, column=1)
            #想改内容
            tk.Label(change_window, text='想改内容').grid(row=4, column=0)
            content_entry = tk.Entry(change_window)
            content_entry.grid(row=4, column=1)
            # 添加提交按钮
            def on_submit():
                name = name_entry.get()
                try:
                    n= int(title_entry.get())-1
                except ValueError:
                    messagebox.showinfo('提示', '请输入数字')
                content = content_entry.get()

                product_sales_instance.update_pro(name,n,content)
                change_window.destroy()

            submit_button = tk.Button(change_window, text='确定', command=on_submit)
            submit_button.grid(row=5, column=1, padx=5, pady=10)

            # 退出按钮
            def on_exit():
                change_window.destroy()
            exit_button = tk.Button(change_window, text='退出', command=on_exit)
            exit_button.grid(row=5, column=0, padx=5, pady=10)
            change_window.mainloop()
        change_button = tk.Button(modify_window, text='修改记录', command=change)
        change_button.grid(row=10, column=1, padx=5, pady=10)
        modify_window.mainloop()

    #删除功能
    def delete(self):
        product_sales_instance = Product_sales()
        delete_window = tk.Tk()
        delete_window.title('删除商品信息')
        delete_window.geometry('500x300')
        #商品类别
        tk.Label(delete_window,text='将删除商品的类别').grid(row=0,column=0)
        category_entry = tk.Entry(delete_window)
        category_entry.grid(row=0,column=1)
        #商品名称
        tk.Label(delete_window,text='将删除商品的名称').grid(row=1,column=0)
        name_entry = tk.Entry(delete_window)
        name_entry.grid(row=1,column=1)

        # 添加提交按钮
        def on_submit():
            category = category_entry.get()
            name = name_entry.get()

            product_sales_instance.delete_pro(category,name)
            delete_window.destroy()

        submit_button = tk.Button(delete_window, text='确定', command=on_submit)
        submit_button.grid(row=2, column=1, padx=5, pady=10)
        # 退出按钮
        def on_exit():
            delete_window.destroy()
        exit_button = tk.Button(delete_window, text='退出', command=on_exit)
        exit_button.grid(row=2, column=0, padx=5, pady=10)
        delete_window.mainloop()

    #统计功能
    def product_sta(self):
        product_sales_instance = Product_sales()
        sta_window = tk.Tk()
        sta_window.title('商品统计')
        sta_window.geometry('250x300')
        a=tk.Label(sta_window,text='         ').grid(row=0,column=0,padx=10,pady=10)
        products = product_sales_instance.all_pro()

        #显示商品详细信息和总数
        def all_pro():
            all_window = tk.Toplevel(sta_window)
            all_window.title('商品详细信息')
            all_window.geometry('500x300')
            sum=0
            # 显示商品详细信息
            tk.Label(all_window,text='商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商').grid(row=0, column=0)
            count = 1
            for line in products:
                tk.Label(all_window, text=line).grid(row=count, column=0)
                count += 1
                sum+=int(line[5])
            tk.Label(all_window,text='商品总库存:'+str(sum)).grid(row=count+1, column=0)
        all_button = tk.Button(sta_window, text='显示商品详细信息', command=all_pro)
        all_button.grid(row=0, column=1,padx=10,pady=10)

        #按价格对商品统计,按照销售价格从小到大排序
        def money():
            money_window = tk.Toplevel(sta_window)
            money_window.title('按价格对商品统计')
            money_window.geometry('500x300')
            result=sorted(products,key=lambda x:x[4])
            # 显示商品详细信息
            tk.Label(money_window,text='商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商').grid(row=0, column=0)
            count = 1
            for line in result:
                tk.Label(money_window, text=line).grid(row=count, column=0)
                count += 1
        money_button = tk.Button(sta_window, text='按价格对商品统计', command=money)
        money_button.grid(row=1, column=1,padx=10,pady=10)

        ##按库存量对商品统计,按照库存量从小到大排序
        def inventory():
            inventory_window = tk.Toplevel(sta_window)
            inventory_window.title('按库存对商品统计')
            inventory_window.geometry('500x300')
            result=sorted(products,key=lambda x:x[5])
            # 显示商品详细信息
            tk.Label(inventory_window,text='商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商').grid(row=0, column=0)
            count = 1
            for line in result:
                tk.Label(inventory_window, text=line).grid(row=count, column=0)
                count += 1
        inventory_button = tk.Button(sta_window, text='按库存对商品统计', command=inventory)
        inventory_button.grid(row=2, column=1,padx=10,pady=10)

        # 按生产厂家对商品统计,按照首字母从小到大排序
        def manufacture():
            manufacture_window = tk.Toplevel(sta_window)
            manufacture_window.title('按库存对商品统计')
            manufacture_window.geometry('500x300')
            result=sorted(products,key=lambda x:x[7])
            # 显示商品详细信息
            tk.Label(manufacture_window,text='商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商').grid(row=0, column=0)
            count = 1
            for line in result:
                tk.Label(manufacture_window, text=line).grid(row=count, column=0)
                count += 1
        inventory_button = tk.Button(sta_window, text='按库存对商品统计', command=inventory)
        inventory_button.grid(row=3, column=1,padx=10,pady=10)

        def on_exit():
            sta_window.destroy()
        exit_button = tk.Button(sta_window, text='退出', command=on_exit)
        exit_button.grid(row=4, column=1,padx=10,pady=10)
        print(products)

        sta_window.mainloop()

    #信息存盘
    def writefile(self):
        product_sales_instance = Product_sales()
        product_sales_instance.save_data()
        #存入成功
        tk.messagebox.showinfo('提示', '存盘成功')

if __name__ == '__main__':
    A = ProductSales()
    A.sales()
    A.product_sales()




为什么选择购买商品,输入我要购买的商品和数量了,在products_data都会有改变,然后再去查询购物记录的时候,是一个空的,没有记录
两个文件的格式商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商
类别 名称 数量 单价 总价格

  • 写回答

2条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-07-02 17:33
    关注

    引自免费微信小程序:皆我百晓生

    您的代码逻辑大致正确,但确实存在一个关键的问题,即当您查询购物记录时,数据并未正确显示。这主要是因为您没有将销售记录(sales_data)与商品数据(products_data)进行正确的关联。

    在您的代码中,当您尝试销售商品时,您正在更新products_data中的库存,但您并没有在sales_data中记录这次销售。因此,当您查询销售记录时,您看到的是空的。

    为了解决这个问题,您需要在销售商品时,同时在sales_data中添加相应的销售记录。您可以通过以下方式实现:

    1. 当您插入商品到products_data时,同时也插入到sales_data(作为默认库存)。
    2. 当您销售商品时,更新products_data的库存,同时在sales_data中添加一条销售记录。

    以下是对您的代码进行修改的部分,用于确保在销售商品时同时更新销售记录:

    insert_pro函数中:

    def insert_pro(self, data):
        self.products_data.append(data)
        self.sales_data.append([data[0], data[2], '0', data[3], '0'])  # 默认为0库存
        self.save_data()
        print(self.products_data)
    

    search_by_name_pro函数中:

    def search_by_name_pro(self, name, count):
        # ...
        if count > int(product[5]):
            messagebox.showerror('错误', '库存不足')
        else:
            # ...
            product[5] = str(int(product[5]) - count)
            
            # Add sale record to sales_data
            flag = 0
            if self.sales_data == []:
                flag = 1
            else:
                for line in self.sales_data:
                    if line[1] == name:  # 如果有库存记录
                        line[2] = str(count)
                        line[4] = str(int(count) * int(product[3]))
                        flag = 0
                        break
                if flag == 1:
                    self.sales_data.append([product[0], name, str(count), product[3], str(int(count) * int(product[3]))])
    
        # ...
    

    这样,当您销售商品时,不仅会在products_data中更新库存,还会在sales_data中添加相应的销售记录。当您查询销售记录时,您应该能看到正确的销售数据。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月2日