wsh_wshkk 2024-07-01 18:53 采纳率: 70%
浏览 4

为什么在主模块开始点击了购买了商品之后,在那个购买商品的函数,测试显示是有这个购买记录的,为什么再去查询销售记录的时候

我的主文件a.py,我想问的是售出部分的函数代码

import tkinter as tk
from tkinter import messagebox
from user_password import UserManager
from product_s import ProductSales

class LoginSystem:
    def __init__(self):
        # 定义全局变量的用户密码字典
        self.userpass = {}

        # 读取文件存入字典
        self.readfile()

        self.root = tk.Tk()
        self.root.title("超市管理系统登录")
        self.root.geometry("500x150+650+80")  # 创建窗口大小
        self.root_1(self.root)

    def show_1(self):
        self.main_window = tk.Tk()  # 使用Toplevel代替Tk
        self.main_window.title("超市关系系统目录")
        self.main_window.geometry("150x450+650+80")

        # 只在类初始化时创建一次ProductSales实例
        self.product_sales = ProductSales()

        def show2_():
            manager = UserManager()  # 创建UserManager实例
            root2 = tk.Toplevel(self.main_window)  # 创建一个新的顶级窗口
            manager.root_2(root2)  # 使用新窗口作为参数调用root_2方法
            root2.mainloop()
            self.main_window.destroy()
            self.show_1()
        button1 = tk.Button(self.main_window, text='用户管理', command=show2_)
        button1.pack(pady=10)

        product_sales = ProductSales()
        #点击buutton2跳转到product_s.py的sales函数
        def open_sales():
            product_sales = ProductSales()
            product_sales.sales()
        button2 = tk.Button(self.main_window, text='销售功能', command=open_sales)
        button2.pack(pady=10)

        def open_product1():
            product_sales = ProductSales()
            product_sales.product_Management()
        button3 =tk.Button(self.main_window,text='商品管理',command=open_product1)
        button3.pack(pady=10)

        def open_product2():
            product_sales = ProductSales()
            product_sales.product_sales()
        button4 = tk.Button(self.main_window,text='售出记录',command=open_product2)
        button4.pack(pady=10)

        self.main_window.mainloop()

    # 取消按钮
    def on_cancel(self):
        self.root.quit()

if __name__ == "__main__":
    app = LoginSystem()

它对应的引用模块是

import tkinter as tk
from tkinter import messagebox

class ProductSales:
    def __init__(self):
        self.products = []
        self.sales_data = []
        self.readfile('products_values','sales_record')

    def readfile(self, profilename, salesfilename):
        with open(profilename, 'r', encoding='utf-8') as file:
            # 读取文件时,保留第一行作为标题
            self.products_header = file.readline().strip()
            lines = file.readlines()
        for line in lines:
            product_data = line.strip().split()
            self.products.append(product_data)

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

     # 销售记录
    def product_sales(self):
        print('1', self.sales_data)
        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():
                result = []
                category = category_entry.get()
                for line in self.sales_data:
                    if line[0] == category:
                        result.append(line)
                # 显示查询到的结果
                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', result)
                for line in result:
                    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)
            for line in self.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():
            print(self.sales_data)
            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()
                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('提示', '没有该商品')

                # 再次存入文件中
                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)

            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()
        print(self.sales_data)

    #销售功能
    def sales(self):
        print(self.products)
        print(self.sales_data)
        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 = int(count_entry.get())
            except ValueError:    #输入的不是数字
                messagebox.showerror('错误', '请输入有效的数量')
                return

            # 遍历所有商品,找到用户输入的商品
            product_found = False
            for product in self.products:
                if product[2] == product_name:
                    product_found = True
                    # 判断库存是否足够
                    if product_count > int(product[5]):
                        messagebox.showerror('错误', '库存不足')
                    else:
                        # 更新库存
                        product[5] = str(int(product[5]) - product_count)
                        # 更新文件
                        with open('products_values', 'w', encoding='utf-8') as file:
                            file.write('商品类别 商品编号 商品名称 进货价格 销售价格 商品库存 供应商名称 生产厂商' + ' \n')
                            for product_1 in self.products:
                                line = ' '.join(product_1) + '\n'
                                file.write(line)
                        flag=0
                        for line in self.sales_data:
                            if line[1] == product_name:
                                line[2]=int(line[2])+product_count
                                line[4]=int(line[2])*int(line[3])
                                flag=0
                                break
                            else:
                                flag = 1
                        if flag==1:
                            self.sales_data.append([product[0], product_name, str(product_count), product[3], str(int(product_count)*int(product[3]))])

                        with open('sales_record', 'w', encoding='utf-8') as file:
                            for sales_data in self.sales_data:
                                if sales_data!=[]:
                                    line = ' '.join(sales_data) + '\n'
                                    file.write(line)
                        # 提示成功
                        messagebox.showinfo('提示', '销售成功')
                        sale_window.destroy()
                        self.sales()
                        return
            if not product_found:
                messagebox.showerror('错误', '没有该商品')
            else:
                 sale_window.destroy()
                 self.sales()
            messagebox.showerror('错误', '没有该商品')

        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()
        print(self.products)
        print(self.sales_data)

    # 商品管理功能
    def product_Management(self):
        print(self.products)
        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()
                self.products.append([category, id, name, price, sale_price, stock, supplier, manufacturer])
                # 写入文件
                with open('products_values', 'a', encoding='utf-8') as file:
                    line = category + ' ' + id + ' ' + name + ' ' + price + ' ' + sale_price + ' ' + stock + ' ' + supplier + ' ' + manufacturer + ' '
                    file.write(line)
                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()
                for line in self.products:
                    product_data = line.strip().split()
                    if category == product_data[0] or name == product_data[2] or manufacturer == product_data[7]:
                        messagebox.showinfo('提示', '查询成功\n' + line)
                        return
                messagebox.showerror('错误', '该记录不存在!')
                return

            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()

if __name__ == '__main__':
    product_sales = ProductSales()
    product_sales.readfile('products_values','sales_record')
    product_sales.product_sta()

为什么在主模块开始点击了购买了商品之后,在那个购买商品的函数,测试显示是有这个购买记录的,为什么再去查询销售记录的时候,显示这个列表为空了

  • 写回答

1条回答 默认 最新

  • 阿里嘎多学长 2024-07-01 19:05
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    从你提供的代码来看,可能存在几个问题导致销售记录在查询时显示为空:

    1. 全局变量的使用问题: 在你的ProductSales类中,sales_dataproducts被定义为实例变量,但在LoginSystem类中,你尝试将它们作为全局变量使用。这可能导致数据没有被正确更新或访问。

    2. 文件读写操作: 你的代码在每次销售操作后都写入了销售记录到文件,但在读取销售记录时,你使用的是readfile方法,这个方法在类初始化时只被调用一次。如果在类初始化之后有新的销售记录产生,这些记录并不会被立即读取进sales_data列表。

    3. 销售记录更新时机: 在你的sales方法中,销售成功后,你调用了self.sales()来重新进入销售窗口,但并没有重新读取销售记录。这意味着即使销售记录被写入了文件,也不会立即反映在sales_data列表中。

    4. 异常处理: 在on_submit函数中,如果输入的数量不是有效的整数,会弹出错误消息并返回,但销售记录可能没有正确更新到sales_data列表。

    为了解决这个问题,你可以尝试以下步骤:

    • 确保在每次需要显示销售记录时,都重新从文件中读取最新的销售数据。
    • 检查sales_data是否在正确的作用域内被更新和访问。
    • 在销售成功后,确保调用一个方法来更新sales_data列表,并在显示销售记录的界面中反映这些更改。

    这里是一个可能的解决方案:

    # 在ProductSales类中添加一个方法来刷新销售记录
    def refresh_sales_data(self):
        self.sales_data = []
        with open('sales_record', 'r', encoding='utf-8') as file:
            self.sales_header = file.readline().strip()
            lines = file.readlines()
            for line in lines:
                sales_data = line.strip().split()
                self.sales_data.append(sales_data)
    
    # 在需要显示销售记录的地方调用这个方法
    def product_sales(self):
        # ... 其他代码 ...
        self.refresh_sales_data()  # 在显示销售记录前刷新数据
        # ... 其他代码 ...
    

    请注意,这只是一个示例,你可能需要根据你的具体逻辑调整代码。希望这能帮助你解决问题。如果需要更详细的帮助,请随时提问。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月1日