PysimpleGUI Calc 未正确使用函数
PysimpleGUI Calc not using function properly
嘿,每个人都在绞尽脑汁想弄清楚我的代码有什么问题,它没有 return 任何错误它似乎只是跳过了计算部分并打印了我包含的错误消息以指示错误的用户输入,即使我的用户输入有效。
我试过移动有关变量的 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, 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()
看起来有些问题
- 使用函数名作为元素的键,如
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, 1)),
sg.Radio("9", 'Guage', size=(8, 1), key='G1', default=True),
sg.Radio("11", 'Guage', size=(8, 1), key='G2'),
sg.Radio("11.5", 'Guage', size=(8, 1), key='G3'),],
[sg.Text('Unit', size=(15, 1)),
sg.Radio("sq ft", 'Unit', size=(8, 1), key='U1', default=True),
sg.Radio("roll", 'Unit', size=(8, 1), key='U2'),
sg.Radio("pallet", 'Unit', size=(8, 1), key='U3'),],
[sg.Text('Amount', size =(15, 1)), 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()
嘿,每个人都在绞尽脑汁想弄清楚我的代码有什么问题,它没有 return 任何错误它似乎只是跳过了计算部分并打印了我包含的错误消息以指示错误的用户输入,即使我的用户输入有效。
我试过移动有关变量的 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, 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()
看起来有些问题
- 使用函数名作为元素的键,如
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, 1)),
sg.Radio("9", 'Guage', size=(8, 1), key='G1', default=True),
sg.Radio("11", 'Guage', size=(8, 1), key='G2'),
sg.Radio("11.5", 'Guage', size=(8, 1), key='G3'),],
[sg.Text('Unit', size=(15, 1)),
sg.Radio("sq ft", 'Unit', size=(8, 1), key='U1', default=True),
sg.Radio("roll", 'Unit', size=(8, 1), key='U2'),
sg.Radio("pallet", 'Unit', size=(8, 1), key='U3'),],
[sg.Text('Amount', size =(15, 1)), 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()