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

PySimpleGUI开发厨房橱柜零件尺寸计算器技术求助

问题解决方案

1. PySimpleGUI适配性确认

PySimpleGUI完全适配你的需求:它语法简洁、新手友好,能快速搭建带表单、标签页的GUI界面,支持数据展示和后续Excel导出功能,非常适合这类小型工具开发。

2. 核心问题解决

(1)Key复用问题

你的代码中layout2存在两个相同的key='Prof2',PySimpleGUI要求所有组件的key必须唯一,否则会导致更新异常或无法正确定位组件。修改方案为每个组件分配唯一key:

# 修改后的layout2
layout2 = [[sg.Text('Ancho Modulo', size=(10,1)),sg.Input('',key='WM')], #Module's width
           [sg.Text('lateral', size=(10,1)),sg.Text('',key='Lat'),sg.Text('x'),sg.Text('',key='Prof2_Lat')], #side pieces depth
           [sg.Text('Piso', size=(10,1)),sg.Text('',key='WM2'),sg.Text('x'),sg.Text('',key='Prof2_Piso')], # bottom piece depth
            [sg.Button('Go'), sg.Button('导出Excel')]]

(2)运算逻辑模块化实现

将每个零件的计算逻辑拆分为独立函数,便于维护和扩展:

# 计算侧板尺寸
def calcular_lateral(altura_modulo, grosor_placa):
    return altura_modulo - grosor_placa

# 计算底板尺寸
def calcular_piso(ancho_modulo, profundidad_modulo, grosor_placa):
    return ancho_modulo, profundidad_modulo - grosor_placa

3. 完整重构代码

整合上述修改,新增Excel导出功能(需先执行pip install pandas openpyxl安装依赖库):

import PySimpleGUI as sg
import pandas as pd

# 零件计算函数
def calcular_lateral(altura_modulo, grosor_placa):
    return altura_modulo - grosor_placa

def calcular_piso(ancho_modulo, profundidad_modulo, grosor_placa):
    return ancho_modulo, profundidad_modulo - grosor_placa

# 导出Excel函数
def exportar_excel(datos):
    df = pd.DataFrame(datos)
    df.to_excel('橱柜零件尺寸.xlsx', index=False, engine='openpyxl')
    sg.popup('导出成功!文件已保存为"橱柜零件尺寸.xlsx"')

# 界面布局
layout1 = [[sg.Text('Altura(模块高度)', size=(15,1)),sg.Input('',key='Alt')],
           [sg.Text('Grosor Placa(板材厚度)', size=(15,1)),sg.Input('18',key='Placa')], # 默认18mm
           [sg.Text('Profundidad(模块深度)', size=(15,1)),sg.Input('',key='Prof')]]

layout2 = [[sg.Text('Ancho Modulo(模块宽度)', size=(15,1)),sg.Input('',key='WM')],
           [sg.Text('Lateral(侧板)', size=(15,1)),sg.Text('',key='Lat'),sg.Text('x'),sg.Text('',key='Prof2_Lat')],
           [sg.Text('Piso(底板)', size=(15,1)),sg.Text('',key='WM2'),sg.Text('x'),sg.Text('',key='Prof2_Piso')],
           [sg.Button('计算'), sg.Button('导出Excel'), sg.Button('关闭')]]

tabgrp = [[sg.TabGroup([[sg.Tab('基础参数', layout1), sg.Tab('零件计算', layout2)]])]]  

window = sg.Window("橱柜零件尺寸计算器", tabgrp)

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, '关闭'):
       break
    elif event == '计算':
        try:
            altura = int(values['Alt'])
            grosor_placa = int(values['Placa'])
            profundidad = int(values['Prof'])
            ancho_modulo = int(values['WM'])
            
            # 计算各零件尺寸
            lat_alt = calcular_lateral(altura, grosor_placa)
            piso_ancho, piso_prof = calcular_piso(ancho_modulo, profundidad, grosor_placa)
            
            # 更新界面显示
            window['Lat'].update(value=str(lat_alt))
            window['Prof2_Lat'].update(value=str(profundidad - grosor_placa))
            window['WM2'].update(value=str(piso_ancho))
            window['Prof2_Piso'].update(value=str(piso_prof))
            
            # 保存计算结果(用于导出)
            resultados = {
                '零件名称': ['侧板', '底板'],
                '尺寸(mm)': [f"{lat_alt} x {profundidad - grosor_placa}", f"{piso_ancho} x {piso_prof}"],
                '备注': ['高度=模块高度-板材厚度', '宽度=模块宽度,深度=模块深度-板材厚度']
            }
        except ValueError:
            sg.popup('请输入有效的数字!')
            continue
    elif event == '导出Excel':
        if 'resultados' in locals():
            exportar_excel(resultados)
        else:
            sg.popup('请先执行计算操作!')

window.close()

4. 关键说明

  • Key唯一性:所有组件的key必须唯一,避免更新时出现定位错误
  • 函数化运算:每个零件的计算逻辑独立封装,后续新增零件(如顶板、背板)时只需添加新函数即可
  • Excel导出:使用pandas简化导出流程,兼容所有Excel版本
  • 用户体验优化:添加默认值、错误提示、操作反馈,提升工具易用性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 06:24:47