844|8

6713

帖子

10

TA的资源

版主

楼主
 

【得捷电子Follow me第2期】任务4-1 日历&时钟的思考 [复制链接]

  本帖最后由 秦天qintian0303 于 2023-9-18 21:29 编辑

        上次写了一个通过互联网更新的万年历时钟,并显示当地的天气信息,是通过互联网获取,然后通过整合到一个字符串进行刷新的,只采用了一种显示信息,显示略有低调。

        本次主要想实现的目标是不同的信息采用不同的颜色,为此我们建立几个刷新的函数:

  • splash = displayio.Group()
  • # 获取数据
  • def get_TimeData():
  • try:
  • response = requests.get(JSON_TIME_URL)
  • except ConnectionError as e:
  • print("Connection Error:", e)
  • print("Retrying in 60 seconds")
  • time_data = response.json()['sysTime2'][:19]
  • return time_data
  • def get_WeatherData():
  • try:
  • city_code = "101091101" # 秦皇岛市的城市代码
  • url = JSON_weather_URL.format(city_code)
  • response = requests.get(url)
  • except ConnectionError as e:
  • print("Connection Error:", e)
  • print("Retrying in 60 seconds")
  • weather_data = response.json()
  • return weather_data
  • def refresh_screen(x, y, dis_str, color):
  • dis_str_area = label.Label(font, text=dis_str, scale=1)
  • dis_str_area.x = x
  • dis_str_area.y = y
  • dis_str_area.color = color
  • splash.append(dis_str_area)
  • def Weather_screen():
  • info_weather = get_WeatherData()
  • cityInfo = info_weather['cityInfo']
  • dis_str = ""+cityInfo['parent']+' '+cityInfo['city']
  • refresh_screen(2, 30, dis_str, color_TurquoiseBlue)
  • def Time_screen():
  • info_time = get_TimeData()
  • refresh_screen(2, 10, info_time, color_WhiteSmoke)
  • display.show(splash)
  • Time_screen()
  • Weather_screen()
  • while True:
  • time.sleep(0.1)
  • Time_cut = Time_cut + 1
  • if Time_cut % 2 == 0:
  • Time_screen()
  • # info_time = get_TimeData()
  • # Allrefresh_screen()
  • if Time_cut % 600 == 0:
  • Weather_screen()
  • # info_weather = get_WeatherData()
  • # Allrefresh_screen()

        效果不是很理想,如下图:

        如圈内现象,不是覆盖而是重合了,查找到原因,splash.append(dis_str_area)就是在原图上显示,有想过一些方法,重新建一个图层,结果时间正常了,不过城市信息被覆盖了,要不就是这样,有没有好的方法可以直接把这部分替换掉?欢迎大神指导。也有通过建立全局变量去保存的,不过应该是超了,直接报错。

        上面办法是分时获取时间和天气数据,那么就通过同时获取来进行,去除秒针显示,60s刷新一次,如下:

  • # 刷新显示
  • def Allrefresh_screen():
  • info_time = get_TimeData()
  • info_weather = get_WeatherData()
  • data_Group = displayio.Group()
  • cityInfo = info_weather['cityInfo']
  • dis_str1 = ""+cityInfo['parent']+' '+cityInfo['city']
  • dis_str1_area = label.Label(font, text=dis_str1, scale=1)
  • dis_str1_area.x = 2
  • dis_str1_area.y = 30
  • dis_str1_area.color = color_LimeGreen
  • data_Group.append(dis_str1_area)
  • dis_str2_area = label.Label(font, text=info_time, scale=1)
  • dis_str2_area.x = 2
  • dis_str2_area.y = 10
  • dis_str2_area.color = color_WhiteSmoke
  • data_Group.append(dis_str2_area)
  • city_weather = info_weather['data']
  • dis_str3 = "今日天气"
  • dis_str3 += ' 空气质量- ' + city_weather['quality']
  • dis_str3 += '\n'+"温度- "+city_weather['wendu']
  • dis_str3 += ' pm2.5-' + str(city_weather['pm25'])
  • dis_str3 += '\n'+"湿度- " + city_weather['shidu']
  • dis_str3 += ' pm10 -' + str(city_weather['pm10'])
  • dis_str3_area = label.Label(font, text=dis_str3, scale=1)
  • dis_str3_area.x = 2
  • dis_str3_area.y = 50
  • dis_str3_area.color = color_TurquoiseBlue
  • data_Group.append(dis_str3_area)
  • display.show(data_Group)
  • while True:
  • time.sleep(0.1)
  • Time_cut = Time_cut + 1
  • if Time_cut % 600 == 0:
  • Allrefresh_screen()

         这次只能一直刷新获取数据并显示,并且采用新建图层刷新显示的方式进行。

        最后还是采用了一分钟获取一次数据并刷新显示,之前有试过一直循坏获取,结果网站直接不给了,应该是触发了什么机制,目前着这样的效果还是可以的,比单一状态要好。

 

 

 

最新回复

# 刷新显示 def Allrefresh_screen(): info_time = get_TimeData() info_weather = get_WeatherData() data_Group = displayio.Group()   cityInfo = info_weather['cityInfo'] dis_str1 = ""+cityInfo['parent']+' '+cityInfo['city'] dis_str1_area = label.Label(font, text=dis_str1, scale=1) dis_str1_area.x = 2 dis_str1_area.y = 30 dis_str1_area.color = color_LimeGreen data_Group.append(dis_str1_area)   dis_str2_area = label.Label(font, text=info_time, scale=1) dis_str2_area.x = 2 dis_str2_area.y = 10 dis_str2_area.color = color_WhiteSmoke data_Group.append(dis_str2_area)   city_weather = info_weather['data'] dis_str3 = "今日天气" dis_str3 += ' 空气质量- ' + city_weather['quality'] dis_str3 += '\n'+"温度- "+city_weather['wendu'] dis_str3 += ' pm2.5-' + str(city_weather['pm25']) dis_str3 += '\n'+"湿度- " + city_weather['shidu'] dis_str3 += ' pm10 -' + str(city_weather['pm10']) dis_str3_area = label.Label(font, text=dis_str3, scale=1) dis_str3_area.x = 2 dis_str3_area.y = 50 dis_str3_area.color = color_TurquoiseBlue data_Group.append(dis_str3_area)   display.show(data_Group)   while True: time.sleep(0.1) Time_cut = Time_cut + 1   if Time_cut % 600 == 0: Allrefresh_screen() 这行是全局更新,你不应该用这个。这个丢while外面应该,给你建立个新的函数,在while里面调用这个更新显示。 # 刷新显示 def Allrefresh_screen_TextUpDate(): dis_str3_area.text=dis_str3 #这个数据你单独重新更新下 dis_str2_area. text=info_time #这个数据你单独重新更新下   详情 回复 发表于 2023-9-21 14:12
点赞 关注
个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

回复
举报

7350

帖子

2

TA的资源

版主

沙发
 

是不是可以给某个区域先刷一层黑矩形,然后再显示?类似清除屏幕内容一样。

点评

那样应该也可以看到刷新的痕迹,我有试过全屏都重新刷,可以看出来一帧一帧的,效果不是很好,现在也是新建图层全屏去覆盖,反而没有那种刷新的效果  详情 回复 发表于 2023-9-20 09:05
 
 
 

回复

6713

帖子

10

TA的资源

版主

板凳
 
wangerxian 发表于 2023-9-19 17:34 是不是可以给某个区域先刷一层黑矩形,然后再显示?类似清除屏幕内容一样。

那样应该也可以看到刷新的痕迹,我有试过全屏都重新刷,可以看出来一帧一帧的,效果不是很好,现在也是新建图层全屏去覆盖,反而没有那种刷新的效果

点评

都是刷新,为啥新建图层就不会有?刷新率应该是一样的才对。  详情 回复 发表于 2023-9-20 09:16
个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

7350

帖子

2

TA的资源

版主

4
 
秦天qintian0303 发表于 2023-9-20 09:05 那样应该也可以看到刷新的痕迹,我有试过全屏都重新刷,可以看出来一帧一帧的,效果不是很好,现在也是新 ...

都是刷新,为啥新建图层就不会有?刷新率应该是一样的才对。

点评

应该是之前的存在了两个图层,写黑和写字,新建的只是一个图层  详情 回复 发表于 2023-9-20 14:59
 
 
 

回复

6713

帖子

10

TA的资源

版主

5
 
wangerxian 发表于 2023-9-20 09:16 都是刷新,为啥新建图层就不会有?刷新率应该是一样的才对。

应该是之前的存在了两个图层,写黑和写字,新建的只是一个图层

点评

这GUI框架还是要好好研究研究呀。效率似乎不是很高的样子。  详情 回复 发表于 2023-9-20 17:49
个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

7350

帖子

2

TA的资源

版主

6
 
秦天qintian0303 发表于 2023-9-20 14:59 应该是之前的存在了两个图层,写黑和写字,新建的只是一个图层

这GUI框架还是要好好研究研究呀。效率似乎不是很高的样子。

点评

还是咱们对这个python了解的不太深,可玩性还是挺足的  详情 回复 发表于 2023-9-21 09:33
 
 
 

回复

6713

帖子

10

TA的资源

版主

7
 
wangerxian 发表于 2023-9-20 17:49 这GUI框架还是要好好研究研究呀。效率似乎不是很高的样子。

还是咱们对这个python了解的不太深,可玩性还是挺足的

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

回复

11

帖子

1

TA的资源

一粒金砂(中级)

8
 

# 刷新显示

def Allrefresh_screen():

info_time = get_TimeData()

info_weather = get_WeatherData()

data_Group = displayio.Group()

 

cityInfo = info_weather['cityInfo']

dis_str1 = ""+cityInfo['parent']+' '+cityInfo['city']

dis_str1_area = label.Label(font, text=dis_str1, scale=1)

dis_str1_area.x = 2

dis_str1_area.y = 30

dis_str1_area.color = color_LimeGreen

data_Group.append(dis_str1_area)

 

dis_str2_area = label.Label(font, text=info_time, scale=1)

dis_str2_area.x = 2

dis_str2_area.y = 10

dis_str2_area.color = color_WhiteSmoke

data_Group.append(dis_str2_area)

 

city_weather = info_weather['data']

dis_str3 = "今日天气"

dis_str3 += ' 空气质量- ' + city_weather['quality']

dis_str3 += '\n'+"温度- "+city_weather['wendu']

dis_str3 += ' pm2.5-' + str(city_weather['pm25'])

dis_str3 += '\n'+"湿度- " + city_weather['shidu']

dis_str3 += ' pm10 -' + str(city_weather['pm10'])

dis_str3_area = label.Label(font, text=dis_str3, scale=1)

dis_str3_area.x = 2

dis_str3_area.y = 50

dis_str3_area.color = color_TurquoiseBlue

data_Group.append(dis_str3_area)

 

display.show(data_Group)

 

while True:

time.sleep(0.1)

Time_cut = Time_cut + 1

 

if Time_cut % 600 == 0:

Allrefresh_screen() 这行是全局更新,你不应该用这个。这个丢while外面应该,给你建立个新的函数,在while里面调用这个更新显示。

# 刷新显示

def Allrefresh_screen_TextUpDate():

dis_str3_area.text=dis_str3 #这个数据你单独重新更新下

dis_str2_area. text=info_time #这个数据你单独重新更新下

点评

回去我试试    详情 回复 发表于 2023-9-21 14:53
 
 
 

回复

6713

帖子

10

TA的资源

版主

9
 
ttxiaotaoge 发表于 2023-9-21 14:12 # 刷新显示 def Allrefresh_screen(): info_time = get_TimeData() info_weather = get_WeatherDa ...

回去我试试  

个人签名

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/10 下一条
艾睿电子& Silicon Labs 有奖直播 | 全新蓝牙信道探测:从技术创新到实际应用
直播时间:3月12日(周三)上午10:00
直播奖励:多功能榨汁机、蓝牙音箱、手机支架

查看 »

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表