Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x PysimpleGUI Calc未正确使用函数_Python 3.x_Calculator_Pysimplegui - Fatal编程技术网

Python 3.x PysimpleGUI Calc未正确使用函数

Python 3.x PysimpleGUI Calc未正确使用函数,python-3.x,calculator,pysimplegui,Python 3.x,Calculator,Pysimplegui,嘿,每个人都在抓我的头,试图找出我的代码有什么问题,它没有返回任何错误,只是似乎跳过了计算部分,并打印了我包含的错误消息,以指示错误的用户输入,即使我的用户输入是有效的 我试着移动if并定义关于变量的语句,看看这是否可行 我知道变量首先在calculate函数上定义,然后又在函数中定义,我之所以这样做是因为,否则我会得到一个错误,它们是未定义的,除非它们首先出现在其他所有变量之上 #Weight Converter Calculator with GUI import PySimpleGUI

嘿,每个人都在抓我的头,试图找出我的代码有什么问题,它没有返回任何错误,只是似乎跳过了计算部分,并打印了我包含的错误消息,以指示错误的用户输入,即使我的用户输入是有效的

我试着移动if并定义关于变量的语句,看看这是否可行

我知道变量首先在calculate函数上定义,然后又在函数中定义,我之所以这样做是因为,否则我会得到一个错误,它们是未定义的,除非它们首先出现在其他所有变量之上


#Weight Converter Calculator with GUI

import PySimpleGUI as sg

def h():
    h = height
def g():
    g = diameter
    if g == 9:
        diameter = .7
    if g == 11:
        diameter == .4
    if g == 11.5:
        diameter = .38    
def u():
    u = typeunit
    if u == ft:
        typeunit = 1
    if u == rl:
        typeunit = 50
    if u == pl:
        typeunit = 450    
def a():
    a = amount

def calc_weight(h, g, u, a):
    try:
        h, g, u, a = float(h), float(g), float(u), float(a)
        h = height
        a = amount
        g = diameter
        u = typeunit
        if g == 9:
            g = .7
        if g == 11:
            g = .4
        if g == 11.5:
            g = .38
        if u == 'ft':
            u = 1
        if u == 'rl':
            u = 50
        if u == 'pl':
            u = 450
        weight = h * g * u * a
        if weight >= 47001:
            standard = 'too heavy for a truck! '
        elif weight <= 47000:
            standard = 'will fit onto a truck! '
    except (ValueError, ZeroDivisionError):
        return None
    else:
        return f'Weight: {weight}, {standard}'

layout = [
    
    [sg.Text('Please enter your desired Mesh Height, Gauge, Unit, Amount')],
    [sg.Text('Mesh Height in FT', size =(15, 1)), sg.Input(key = h)],
    [sg.Text('Gauge 9, 11, 11.5', size =(15, 1)), sg.Input(key = g)],
    [sg.Text('Unit "ft" for sq ft, "rl" for roll, and "pl" for pallet', size =(15, 3)), sg.Input(key = u)],
    [sg.Text('Amount', size =(15, 1)), sg.Input(key = a)],
    [sg.Text('', key='weight', size=(20, 2))],
    [sg.Submit(), sg.Cancel()]
]
 
window = sg.Window('Chain Link Weight Calculator', layout)
sg.theme('DarkAmber') 
while True:
    event, value = window.Read()
    if event == 'Submit':
        weight = calc_weight(value[h], value[g], value[u], value[a],)
        if weight:
            window.Element('weight').Update(weight, text_color='white')
        else:
            window.Element('weight').Update('Input is incorrect! ', text_color='red')
    elif event == 'Cancel':
        break
    
window.Close()

#带GUI的重量转换器计算器
将PySimpleGUI导入为sg
def h():
h=高度
def g():
g=直径
如果g==9:
直径=.7
如果g==11:
直径==.4
如果g==11.5:
直径=.38
def u():
u=类型单位
如果u==英尺:
类型单位=1
如果u==rl:
类型单位=50
如果u==pl:
类型单位=450
定义a():
a=金额
def calc_重量(h、g、u、a):
尝试:
h、 g,u,a=浮点数(h),浮点数(g),浮点数(u),浮点数(a)
h=高度
a=金额
g=直径
u=类型单位
如果g==9:
g=0.7
如果g==11:
g=.4
如果g==11.5:
g=.38
如果u=='ft':
u=1
如果u=='rl':
u=50
如果u=='pl':
u=450
重量=h*g*u*a
如果重量>=47001:
标准=“对卡车来说太重了!”

elif weight看起来有一些问题

  • 使用函数名作为元素的键,如
    h
    g
    u
    a
    ,这些函数都是无用的
  • 变量
    高度
    金额
    直径
    类型单位
    未在函数
    计算重量
  • 变量
    h
    g
    u
    a
    在函数中重置
    计算重量
这里只是演示脚本

将PySimpleGUI导入为sg
def find_true(顺序):
返回序列索引(True)
def calc_重量(值):
尝试:
h、 a=浮动(值['h']),浮动(值['a'])
index1=find_true([key in('G1','G2','G3')的值[key]))
index2=find_true([key in('U1','U2','U3')的值[key]))
g、 u=量规[index1],单位[index2]
重量=h*g*u*a
标准=“对卡车来说太重了!”如果重量>=47001,则“其他”将安装到卡车上
结果=f'Weight:{Weight},{standard}'
除:
结果=无
返回结果
量规=[0.7,0.4,0.38]
单位=[1,50,450]
sg.主题(“达坎伯”)
布局=[
[sg.Text('请输入所需的网格高度、规格、单位、数量'),
[sg.Text('网格高度单位为英尺',大小=(15,1)),sg.Input(key='h'),
[sg.Text('仪表',尺寸=(15,1)),
sg.Radio(“9”,“量规”,大小=(8,1),键='G1',默认值=真),
sg.Radio(“11”,标准尺,尺寸=(8,1),键='G2'),
sg.Radio(“11.5”,“量规”,尺寸=(8,1),键='G3'),],
[sg.Text('单位',大小=(15,1)),
sg.无线电(“平方英尺”,“单位”,大小=(8,1),键='U1',默认值=真),
sg.无线电(“滚动”,“单位”,尺寸=(8,1),键='U2'),
sg.无线电(“托盘”,“单元”,尺寸=(8,1),键='U3'),],
[sg.Text('Amount',size=(15,1)),sg.Input(key='a'),
[sg.Text('',key='weight',size=(45,2))],
[sg.Submit(),sg.Cancel()]
]
窗口=sg.窗口(“链节重量计算器”,布局)
尽管如此:
事件,值=window.Read()
如果事件发生(sg.WINDOW_关闭,“取消”):
打破
elif事件==“提交”:
重量=计算重量(数值)
如果重量:
窗口['weight']。更新(weight,text_color='white')
其他:
窗口['weight']。更新('Input is error!',text_color='white'))
window.Close()

您的
计算重量
功能是否独立工作?我的意思是,给定函数的一组有效输入,它会产生期望的结果吗?因为原始的
calc_weight
使用来自simplegui的
value[]
键,我如何使用用户输入单独测试该函数?您可能是对的,实际函数@itprorh66:pI很可能会出现问题,首先只需单独测试函数,并为其提供一系列有效输入值以及一些无效值,以确保函数正确计算。如果函数工作正常,那么您可以专注于value[]函数,以确保它能满足您的需求。非常感谢您,这太棒了!我更改了仪表的值以准确计算其权重如果我理解正确,您将仪表和单位值分配给了索引,并且(称为)?他们在用收音机吗?再次感谢您的时间和帮助!如果选择了哪个
sg.Radio
,只需确定哪个值。当然,您也可以对指定的标签/值使用
sg.Listbox