编程中遇到的小问题请高手指教
import easygui as gclass Centigrade:
# 初始化对象
def __init__(self,value = 0):
self.value = float(value)
# 查看摄氏温度属性
def __get__(self,instance,value):
return self.value
# 设置摄氏温度属性
def __set__(self,instance,value):
self.value = float(value)
class Fahrenheit:
# 查看华氏温度属性
def __get__(self,instance,value):
return instance.cel * 1.8 + 32
# 设置华氏温度属性
def __set__(self,instance,value):
instance.cel = (float(value) - 32) / 1.8
class Temp:
# 赋值实例对象
cel = Centigrade()
fah = Fahrenheit()
def cel_to_fah():
g.msgbox('欢迎使用摄氏温度转华氏温度')
t = Temp()
t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
g.msgbox('华氏温度为:' + str(t.fah))
def fah_to_cel():
g.msgbox('欢迎使用华氏温度转摄氏温度')
t = Temp()
t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
g.msgbox('摄氏温度为:' + str(t.cel))
g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
if True:
cel_to_fah()
else:
fah_to_cel()学习了Property原理后写了段代码运行后ccbox无论按那个按钮都运行摄氏转华氏,不知为什么?请问如何改正!
if True:
cel_to_fah()
只能进True
import easygui as g
class Centigrade:
# 初始化对象
def __init__(self,value = 0):
self.value = float(value)
# 查看摄氏温度属性
def __get__(self,instance,value):
return self.value
# 设置摄氏温度属性
def __set__(self,instance,value):
self.value = float(value)
class Fahrenheit:
# 查看华氏温度属性
def __get__(self,instance,value):
return instance.cel * 1.8 + 32
# 设置华氏温度属性
def __set__(self,instance,value):
instance.cel = (float(value) - 32) / 1.8
class Temp:
# 赋值实例对象
cel = Centigrade()
fah = Fahrenheit()
def cel_to_fah():
g.msgbox('欢迎使用摄氏温度转华氏温度')
t = Temp()
t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
g.msgbox('华氏温度为:' + str(t.fah))
def fah_to_cel():
g.msgbox('欢迎使用华氏温度转摄氏温度')
t = Temp()
t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
g.msgbox('摄氏温度为:' + str(t.cel))
ans=g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
if ans:
cel_to_fah()
else:
fah_to_cel()
页:
[1]