我正在尝试存储变量的先前值,其变量已通过对话框更改,然后如果取消对话框并返回None
,则将该变量的值转换回先前设置的值。例如,考虑:
letter_height = 30 letter_width = 50
我希望用户通过此更改值,这确实有效:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
listen()
这被称为:
onkey(NewLetterDimensions, "k")
但是,如果对话框被取消,我希望“if”语句能够返回存储的先前值,以便将变量更改为先前的值而不是None
。那么,我将如何实现这一目标呢?所以,我想要这个:
def NewLetterDimensions():
global letter_height
letter_height = (numinput("New Letter Height", "Please set the new letter height: ", minval = 10, maxval = 170))
if letter_height == None:
[] <- # Return previously set letter height (whether from previous dialog or not)
listen()
global letter_width
letter_width = (numinput("New Letter Width", "Please set the new letter width: ", minval = 10, maxval = 170))
if letter_width == None:
[] <- # Return previously set letter width (whether from previous dialog or not)
listen()
是的,这是龟图形。
答案 0 :(得分:2)
怎么样:
user_input = (numinput("New Letter Height", "Please set the new letter height: ",
minval=10, maxval=170))
letter_height = letter_height if user_input is None else user_input
仅当letter_height
不是user_input
时才会更改为None
,否则会保留旧值。
BTW,始终将None
与is
测试进行比较PEP8建议:
与
None
等单身人士的比较应始终使用is
或is not
,而不是等同运算符。