如何解决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()
解决方法
好像有问题
- 使用函数名作为元素的键,比如
h
、g
、u
和a
,这些函数没用 - 变量
height
、amount
、diameter
和typeunit
未在函数calc_weight
中定义 - 变量
h
、g
、u
、a
在函数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()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。