PysimpleGUI Calc 未正确使用功能

如何解决PysimpleGUI Calc 未正确使用功能

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

我已经尝试移动有关变量的 if 和定义语句,看看这是否可行。

我知道变量首先在计算函数上方定义,然后在函数中再次定义,我这样做的原因是因为否则我会得到一个错误,即它们未定义,除非它们首先出现在其他一切之上。


#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,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',sg.Input(key = g)],[sg.Text('Unit "ft" for sq ft,"rl" for roll,and "pl" for pallet',3)),sg.Input(key = u)],[sg.Text('Amount',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()

解决方法

好像有问题

  • 使用函数名作为元素的键,比如hgua,这些函数没用
  • 变量 heightamountdiametertypeunit 未在函数 calc_weight 中定义
  • 变量 hgua 在函数 calc_weight 中重置

这里只是演示脚本,

import PySimpleGUI as sg

def find_true(sequence):
    return sequence.index(True)

def calc_weight(values):
    try:
        h,a = float(values['h']),float(values['a'])
        index1 = find_true([values[key] for key in ('G1','G2','G3')])
        index2 = find_true([values[key] for key in ('U1','U2','U3')])
        g,u = guages[index1],units[index2]
        weight = h*g*u*a
        standard = 'too heavy for a truck!' if weight>= 47001 else 'will fit onto a truck!'
        result = f'Weight: {weight},{standard}'
    except:
        result = None
    return result

guages = [0.7,0.4,0.38]
units  = [1,50,450]

sg.theme('DarkAmber')

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',size=(15,sg.Radio("9",'Guage',size=(8,1),key='G1',default=True),sg.Radio("11",key='G2'),sg.Radio("11.5",key='G3'),],[sg.Text('Unit',sg.Radio("sq ft",'Unit',key='U1',sg.Radio("roll",key='U2'),sg.Radio("pallet",key='U3'),[sg.Text('Amount',sg.Input(key='a')],[sg.Text('',key='weight',size=(45,2))],[sg.Submit(),sg.Cancel()]
]

window = sg.Window('Chain Link Weight Calculator',layout)

while True:

    event,values = window.Read()

    if event in (sg.WINDOW_CLOSED,'Cancel'):
        break
    elif event == 'Submit':
        weight = calc_weight(values)
        if weight:
            window['weight'].update(weight,text_color='white')
        else:
            window['weight'].update('Input is incorrect!',text_color='white')

window.Close()

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?