零部件仓库信息管理小程序

import tkinter as tk
from tkinter import ttk
import datetime
import csv
from tkinter import messagebox



class LoginWindow(tk.Toplevel):
    def __init__(self, master):
        super().__init__(master)
        self.master.withdraw()
        self.title("管理员登录")
        self.geometry("300x180")
        self.resizable(False, False)

        self.lbl_username = tk.Label(self, text="用户名:")
        self.lbl_username.pack(pady=5)
        self.ent_username = tk.Entry(self)
        self.ent_username.pack(pady=5)

        self.lbl_password = tk.Label(self, text="密码:")
        self.lbl_password.pack(pady=5)
        self.ent_password = tk.Entry(self, show="*")
        self.ent_password.pack(pady=5)

        self.btn_login = tk.Button(self, text="登录", command=self.login)
        self.btn_login.pack(pady=10)

    def login(self):
        username = self.ent_username.get()
        password = self.ent_password.get()

        # Check if the username and password are correct
        if username == "admin" and password == "password":
            # Set the logged_in attribute to True
            self.logged_in = True
            # Close the login window
            self.destroy()
        else:
            messagebox.showerror("错误", "用户名或密码错误,请重试。")

class MainWindow(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("仓库管理系统")
        self.geometry("1500x1200")
        self.resizable(False, False)
        self.config(bg="#ADD8E6")



        # 创建标签和文本框
        lbl_part_no = tk.Label(self, text="零件编号",font=("Arial", 12), bg="#ADD8E5")
        lbl_part_no.grid(row=0, column=0, padx=10, pady=10)
        ent_part_no = tk.Entry(self, font=("Arial", 12))
        ent_part_no.grid(row=0, column=1, padx=10, pady=10)
        
        lbl_bom_no = tk.Label(self, text="BOM号",font=("Arial", 12), bg="#ADD8E3")
        lbl_bom_no.grid(row=2, column=0, padx=10, pady=10)
        ent_bom_no = tk.Entry(self, font=("Arial", 12))
        ent_bom_no.grid(row=2, column=1, padx=10, pady=10)
        
        lbl_time_no = tk.Label(self, text="入库时间",font=("Arial", 12), bg="#ADD8E3")
        lbl_time_no.grid(row=4, column=0, padx=5, pady=5)
        ent_time_no = tk.Entry(self, font=("Arial", 12))
        ent_time_no.grid(row=4, column=1, padx=10, pady=10)
        
        # Define the options for the "零件类型" and "项目" fields
        part_type_options = ["减速器", "CVT", "电驱总成", "电机", "半轴", "油液", "链条", "钢带", "齿轮", "轴承", "轴", "其他"]
        project_options = ["EMR3", "Qogir", "VT2i", "VT3", "VT5", "预研项目", "其他"]
        
        # Create the "零件类型" combobox
        lbl_part_type = tk.Label(self, text="零件类型",font=("Arial", 12), bg="#ADD8E3")
        lbl_part_type.grid(row=1, column=0, padx=10, pady=10)
        ent_part_type = ttk.Combobox(self, values=part_type_options, font=("Arial", 12))
        ent_part_type.grid(row=1, column=1, padx=10, pady=10)
        
        # Create the "项目" combobox
        lbl_project_no = tk.Label(self, text="项目",font=("Arial", 12), bg="#ADD8E3")
        lbl_project_no.grid(row=3, column=0, padx=10, pady=10)
        ent_project_no = ttk.Combobox(self, values=project_options, font=("Arial", 12))
        ent_project_no.grid(row=3, column=1, padx=10, pady=10)
        
        
        # 创建按钮
        btn_add = tk.Button(self, text="入库", command=lambda: add_part())
        btn_add.grid(row=5, column=0, padx=5, pady=5)
        
        btn_remove = tk.Button(self, text="删除", command=lambda: remove_part())
        btn_remove.grid(row=5, column=1, padx=5, pady=5)
        
        btn_query = tk.Button(self, text="查询", command=lambda: query_parts())
        btn_query.grid(row=5, column=2, padx=5, pady=5)
        
        btn_checkout = tk.Button(self, text="出库", command=lambda: checkout_part())
        btn_checkout.grid(row=5, column=3, padx=5, pady=5)
        
        btn_filter = tk.Button(self, text="筛选", command=lambda: filter_parts())
        btn_filter.grid(row=5, column=4, padx=5, pady=5)
        
        btn_all = tk.Button(self, text="显示全部", command=lambda: all_parts())
        btn_all.grid(row=5, column=5, padx=5, pady=5)
        
        #创建表格
        cols = ("零件编号", "零件类型", "BOM号", "项目", "入库时间","入库时长") 
        tree = tk.ttk.Treeview(self, columns=cols, show="headings") 
        for col in cols: 
            tree.heading(col, text=col, anchor="center") # Add anchor="center" to center the values
        tree.grid(row=6, column=0, columnspan=5, padx=5, pady=5)

        
        def update_table():
            # Clear the current table
            for item in tree.get_children():
                tree.delete(item)
        
            # Read the data from the CSV file and insert it into the table
            with open("C:\python\warehouse\parts.csv", mode="r") as file:
                reader = csv.reader(file)
                next(reader)
                for row in reader:
                    tree.insert("", "end", values=row)
        
        def add_part(): 
            part_no = ent_part_no.get() 
            part_type = ent_part_type.get() 
            bom_no = ent_bom_no.get() 
            time_no = ent_time_no.get()
            project_no = ent_project_no.get()
            # 计算时间并更新表格
            entry_time = datetime.datetime.strptime(time_no, "%Y-%m-%d")
            today = datetime.datetime.now()
            months = (today.year - entry_time.year) * 12 + (today.month - entry_time.month)
            if today.day < entry_time.day:
                months -= 1
        
        
            if part_type in part_type_options and part_no and bom_no and time_no: 
                with open("C:\python\warehouse\parts.csv", mode="a", newline="") as file:
                    writer = csv.writer(file) 
                    writer.writerow([part_no.center(10), part_type.center(10), bom_no.center(10), time_no.center(10), project_no.center(10), f"{months}个月".center(10)])
                    messagebox.showinfo("提示", "零件信息已保存") 
            elif part_type not in part_type_options:
                messagebox.showwarning("警告", "请选择正确的零件类型")
            else: 
                messagebox.showwarning("警告", "请输入完整的零件信息")
        
            update_table()
        
        def filter_parts():
            part_no = ent_part_no.get()
            part_type = ent_part_type.get()
            bom_no = ent_bom_no.get()
            project_no = ent_project_no.get()
            time_no = ent_time_no.get()
        
            # 清除当前表格
            for item in tree.get_children():
                tree.delete(item)
        
            # 从CSV文件中读取数据并将其插入表格
            with open("C:\python\warehouse\parts.csv", mode="r") as file:
                reader = csv.reader(file)
                next(reader)
                for row in reader:
                    if (not part_no or part_no in row[0]) and \
                       (not part_type or part_type in row[1]) and \
                       (not bom_no or bom_no in row[2]) and \
                       (not project_no or project_no in row[4]) and \
                       (not time_no or time_no in row[3]):
                        tree.insert("", "end", values=row)
        
        
        def all_parts():
            update_table()
        
        
        # 从文件中删除零件信息
        def remove_part():
            cur_item = tree.focus()
            if cur_item:
                # 获取所选项目的零件号
                part_no = tree.item(cur_item, "values")[0]
                # 从 CSV 文件中读取数据并过滤掉所选项目的行
                with open("C:\python\warehouse\parts.csv", mode="r") as file:
                    reader = csv.reader(file)
                    header = next(reader)
                    rows = [row for row in reader if row[0] != part_no]
                # 将过滤后的数据写回 CSV 文件
                with open("C:\python\warehouse\parts.csv", mode="w", newline="") as file:
                    writer = csv.writer(file)
                    writer.writerow(header)
                    writer.writerows(rows)
                # 从表格中删除所选项目
                tree.delete(cur_item)
                messagebox.showinfo("提示", "零件信息已删除")
                update_table()
            else:
                messagebox.showwarning("警告", "请选择要删除的零件信息")


        
        
        
        #从文件中查询零件信息
        def query_parts(): 
            for item in tree.get_children(): 
                tree.delete(item) 
            with open("C:\python\warehouse\parts.csv", mode="r") as file:       
                reader = csv.reader(file) 
                next(reader) 
                for row in reader: 
                    tree.insert("", "end", values=row)
        
        # 将零件出库并更新文件中的信息
        def checkout_part():
            cur_item = tree.focus()
            if cur_item:
                part_no = tree.item(cur_item, "values")[0]
                with open("C:\python\warehouse\parts.csv", mode="r") as file:      
                    lines = file.readlines()
                with open("C:\python\warehouse\parts.csv", mode="w", newline="") as file:
                    writer = csv.writer(file)
                    for line in lines:
                        if line.startswith(part_no):
                            parts = line.strip().split(",")
                            parts[3] = "出库"
                            writer.writerow(parts)
                        else:
                            writer.writerow(line.strip().split(","))
                tree.item(cur_item, values=(part_no, parts[1], parts[2], parts[3]))
                messagebox.showinfo("提示", "零件已出库")
            else:
                messagebox.showwarning("警告", "请选择要出库的零件信息")
            update_table()


def run():
    #print("Starting run() function")
    # Create an instance of the LoginWindow class
    login_window = LoginWindow(None)

    # Wait for the login window to be closed
    login_window.wait_window()

    # Check if the user has successfully logged in
    if login_window.logged_in:
        # Create an instance of the MainWindow class
        main_window = MainWindow()

        # Run the main loop
        main_window.mainloop()

run()

目前是用绝对路径目录运行的,索引需要将parts.csv文件放在c:/python/warehouse 目录下,如果需要存储在其他目录下也可以修改代码中的路径。直接Ctrl+F替换语句即可。

with open("C:\python\warehouse\parts.csv", mode="r") as file:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值