You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让USB条码扫描器无需按回车即可完成库存出入库操作?

解决方案

核心思路

利用USB条码扫描器**扫描完成后自动发送回车(Enter)**的特性(绝大多数扫描器默认支持,若未开启可通过扫描器说明书设置),结合tkinter的事件绑定,根据当前选中的操作模式(入库/出库)自动执行对应逻辑,无需手动点击按钮。

实现代码

from DB_CodeReader import Bobina, session                              # 你的数据库模型
from sqlalchemy import create_engine, select, exists, update
import pandas as pd
from tkinter import *
import tkinter as tk
from tkinter import ttk
from datetime import date
import sys, os
from barcode import EAN13

# 主窗口初始化
root = tk.Tk()
root.title("库存管理系统")

# 当前操作模式:None=普通模式,'in'=入库模式,'out'=出库模式
current_mode = tk.StringVar(value=None)

# 条码输入框
barcode_entry = ttk.Entry(root, font=('Arial', 14), width=20)
barcode_entry.pack(pady=20)
barcode_entry.focus_set()  # 默认聚焦输入框,方便扫描

# -------------------------- 操作函数 --------------------------
def handle_barcode_scan(event):
    """扫描条码后的自动处理逻辑"""
    barcode = barcode_entry.get().strip()
    if not barcode:
        return
    
    mode = current_mode.get()
    if mode == 'in':
        # 执行入库操作
        stock_in(barcode)
    elif mode == 'out':
        # 执行出库操作
        stock_out(barcode)
    
    # 清空输入框,准备下一次扫描
    barcode_entry.delete(0, END)

def stock_in(barcode):
    """入库逻辑:若条码存在则增加库存,不存在则新建记录"""
    # 查询是否已有该条码的库存
    existing_item = session.query(Bobina).filter(Bobina.barcode == barcode).first()
    if existing_item:
        # 库存+1(可根据需求修改为批量数量)
        existing_item.quantity += 1
        existing_item.update_date = date.today()
    else:
        # 创建新的库存记录(需根据你的Bobina模型字段调整)
        new_item = Bobina(
            barcode=barcode,
            quantity=1,
            create_date=date.today(),
            update_date=date.today()
        )
        session.add(new_item)
    session.commit()
    print(f"条码 {barcode} 入库成功")

def stock_out(barcode):
    """出库逻辑:检查库存足够则减少,不足则提示"""
    existing_item = session.query(Bobina).filter(Bobina.barcode == barcode).first()
    if not existing_item:
        print(f"条码 {barcode} 不存在,无法出库")
        return
    if existing_item.quantity <= 0:
        print(f"条码 {barcode} 库存不足,无法出库")
        return
    
    existing_item.quantity -= 1
    existing_item.update_date = date.today()
    session.commit()
    print(f"条码 {barcode} 出库成功")

def set_mode(mode):
    """切换操作模式,并更新按钮视觉反馈"""
    current_mode.set(mode)
    # 重置所有按钮样式
    for btn in [stock_in_btn, stock_out_btn, reset_btn]:
        btn.config(style='TButton')
    # 给当前模式按钮加高亮
    if mode == 'in':
        stock_in_btn.config(style='Success.TButton')
    elif mode == 'out':
        stock_out_btn.config(style='Danger.TButton')
    else:
        reset_btn.config(style='Info.TButton')

# -------------------------- 界面组件 --------------------------
# 自定义按钮样式
style = ttk.Style()
style.configure('Success.TButton', background='#2ecc71', foreground='white')
style.configure('Danger.TButton', background='#e74c3c', foreground='white')
style.configure('Info.TButton', background='#3498db', foreground='white')

button_frame = ttk.Frame(root)
button_frame.pack(pady=10)

stock_in_btn = ttk.Button(button_frame, text="进入入库模式", command=lambda: set_mode('in'))
stock_in_btn.grid(row=0, column=0, padx=10)

stock_out_btn = ttk.Button(button_frame, text="进入出库模式", command=lambda: set_mode('out'))
stock_out_btn.grid(row=0, column=1, padx=10)

reset_btn = ttk.Button(button_frame, text="退出自动模式", command=lambda: set_mode(None))
reset_btn.grid(row=0, column=2, padx=10)

# 绑定回车事件(扫描器完成扫描后触发)
barcode_entry.bind('<Return>', handle_barcode_scan)

# 主循环
root.mainloop()

关键说明

  1. 模式切换:通过current_mode字符串变量记录当前操作状态,点击按钮时调用set_mode()切换,同时修改按钮样式提供视觉反馈,避免用户混淆当前模式。
  2. 扫描触发逻辑:绑定输入框的<Return>事件,扫描器完成条码输入后会自动发送回车,触发handle_barcode_scan()函数,自动执行对应入库/出库操作。
  3. 数据库操作:
    • 入库:查询条码是否存在,存在则增加库存,不存在则新建记录(需根据你的Bobina模型字段调整字段名,比如quantity是库存数量,create_date是创建日期等)。
    • 出库:先检查条码是否存在、库存是否充足,满足条件则减少库存。
  4. 用户体验优化:扫描完成后自动清空输入框,默认聚焦输入框,无需手动点击准备下一次扫描。

注意事项

  • 若你的条码扫描器没有自动发送回车,可查看扫描器说明书,通过扫描设置条码开启“后缀加回车”功能,这是实现自动触发的核心。
  • 数据库操作需确保Bobina模型的字段与代码中的调用一致,若字段名不同需对应修改。
  • 可根据需求扩展批量入库/出库逻辑,比如在输入框中同时输入条码和数量,拆分后处理。

内容的提问来源于stack exchange,提问作者Leandro Bertarelli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:33:25