鱼C论坛

 找回密码
 立即注册
查看: 790|回复: 5

[已解决]新手一枚,编程中遇到的小问题请高手指教!

[复制链接]
发表于 2019-1-7 15:12:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import easygui as g
  2. class Centigrade:
  3.       
  4.       # 初始化对象
  5.       def __init__(self,value = 0):
  6.             self.value = float(value)

  7.       # 查看摄氏温度属性
  8.       def __get__(self,instance,value):
  9.             return self.value
  10.       
  11.       # 设置摄氏温度属性
  12.       def __set__(self,instance,value):
  13.             self.value = float(value)

  14. class Fahrenheit:

  15.       # 查看华氏温度属性
  16.       def __get__(self,instance,value):
  17.             return instance.cel * 1.8 + 32
  18.       
  19.       # 设置华氏温度属性
  20.       def __set__(self,instance,value):
  21.             instance.cel = (float(value) - 32) / 1.8

  22. class Temp:
  23.       # 赋值实例对象
  24.       cel = Centigrade()
  25.       fah = Fahrenheit()

  26. def cel_to_fah():
  27.       g.msgbox('欢迎使用摄氏温度转华氏温度')
  28.       t = Temp()
  29.       t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
  30.       g.msgbox('华氏温度为:' + str(t.fah))

  31. def fah_to_cel():
  32.       g.msgbox('欢迎使用华氏温度转摄氏温度')
  33.       t = Temp()
  34.       t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
  35.       g.msgbox('摄氏温度为:' + str(t.cel))
  36.       

  37. g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
  38. if True:
  39.       cel_to_fah()
  40. else:
  41.       fah_to_cel()
复制代码


学习了Property原理后写了段代码运行后ccbox无论按那个按钮都运行摄氏转华氏,不知为什么?请问如何改正!!
最佳答案
2019-1-7 15:19:58
  1. import easygui as g
  2. class Centigrade:
  3.       
  4.       # 初始化对象
  5.       def __init__(self,value = 0):
  6.             self.value = float(value)

  7.       # 查看摄氏温度属性
  8.       def __get__(self,instance,value):
  9.             return self.value
  10.       
  11.       # 设置摄氏温度属性
  12.       def __set__(self,instance,value):
  13.             self.value = float(value)

  14. class Fahrenheit:

  15.       # 查看华氏温度属性
  16.       def __get__(self,instance,value):
  17.             return instance.cel * 1.8 + 32
  18.       
  19.       # 设置华氏温度属性
  20.       def __set__(self,instance,value):
  21.             instance.cel = (float(value) - 32) / 1.8

  22. class Temp:
  23.       # 赋值实例对象
  24.       cel = Centigrade()
  25.       fah = Fahrenheit()

  26. def cel_to_fah():
  27.       g.msgbox('欢迎使用摄氏温度转华氏温度')
  28.       t = Temp()
  29.       t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
  30.       g.msgbox('华氏温度为:' + str(t.fah))

  31. def fah_to_cel():
  32.       g.msgbox('欢迎使用华氏温度转摄氏温度')
  33.       t = Temp()
  34.       t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
  35.       g.msgbox('摄氏温度为:' + str(t.cel))
  36.       

  37. ans=g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
  38. if ans:
  39.       cel_to_fah()
  40. else:
  41.       fah_to_cel()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-7 15:16:52 | 显示全部楼层
if True:
      cel_to_fah()
只能进True
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-7 15:19:58 | 显示全部楼层    本楼为最佳答案   
  1. import easygui as g
  2. class Centigrade:
  3.       
  4.       # 初始化对象
  5.       def __init__(self,value = 0):
  6.             self.value = float(value)

  7.       # 查看摄氏温度属性
  8.       def __get__(self,instance,value):
  9.             return self.value
  10.       
  11.       # 设置摄氏温度属性
  12.       def __set__(self,instance,value):
  13.             self.value = float(value)

  14. class Fahrenheit:

  15.       # 查看华氏温度属性
  16.       def __get__(self,instance,value):
  17.             return instance.cel * 1.8 + 32
  18.       
  19.       # 设置华氏温度属性
  20.       def __set__(self,instance,value):
  21.             instance.cel = (float(value) - 32) / 1.8

  22. class Temp:
  23.       # 赋值实例对象
  24.       cel = Centigrade()
  25.       fah = Fahrenheit()

  26. def cel_to_fah():
  27.       g.msgbox('欢迎使用摄氏温度转华氏温度')
  28.       t = Temp()
  29.       t.cel = g.integerbox(msg="摄氏温度是:",title="",lowerbound=-100,upperbound=100)
  30.       g.msgbox('华氏温度为:' + str(t.fah))

  31. def fah_to_cel():
  32.       g.msgbox('欢迎使用华氏温度转摄氏温度')
  33.       t = Temp()
  34.       t.fah = g.integerbox(msg="华氏温度是:",title="",lowerbound=-100,upperbound=100)
  35.       g.msgbox('摄氏温度为:' + str(t.cel))
  36.       

  37. ans=g.ccbox(choices = ('摄氏转华氏','华氏转摄氏'))
  38. if ans:
  39.       cel_to_fah()
  40. else:
  41.       fah_to_cel()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-9 10:42:07 | 显示全部楼层

我试了ccbox如果按cancel返回的是False怎么我写的不行呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-9 10:48:11 | 显示全部楼层
你写的不是按ccbox输出,而就是对BOOL常量判断,if True肯定进这个if啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2019-1-11 10:39:31 | 显示全部楼层
塔利班 发表于 2019-1-9 10:48
你写的不是按ccbox输出,而就是对BOOL常量判断,if True肯定进这个if啊

明白了,谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-6 02:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表