0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

KIVYで温度・湿度・気圧などの情報を表示する

Posted at

ラズパイにディスプレイをくっつけてセンサーの値を表示しようとした。
Pythonで書くのが慣れていて楽だったのでPython用のGUIToolKitからKIVYを選定

ただ表示するだけでなく、一定間隔で表示を更新したかったため、threadingも使用。

display.py
#!/usr/bin/env python3

import threading
from time import sleep
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
# noinspection PyProtectedMember
from kivy.core.text import LabelBase, DEFAULT_FONT
from datetime import datetime
import sensors
import json

LabelBase.register(DEFAULT_FONT, "IPAexfont00301/ipaexg.ttf")

# _get_temperature()等の関数は自分で用意すること

class Display(BoxLayout):

    def main(self):
        while True:
            hour = datetime.now().hour
            if hour > 23 or hour < 7:
                text_color = [.5, .5, .5, 1]
            else:
                text_color = [1, 1, 1, 1]
            self.ids.time.text = datetime.now().strftime("%H:%M")
            temp = _get_temperature()
            temp2 = _get_temperature2()
            temp_str = temp if type(temp) is str else u'{:.1f}'.format(temp)
            temp2_str = temp2 if type(temp2) is str else u'{:.1f}'.format(temp2)
            self.ids.temp.text = u'温度 内 {0} ℃ 外 {1} ℃'.format(temp_str, temp2_str)
            self.ids.temp.color = text_color
            humid = _get_humid()
            humid2 = _get_humid2()
            humid_str = humid if type(humid) is str else u'{:.1f}'.format(humid)
            humid2_str = humid2 if type(humid2) is str else u'{:.1f}'.format(humid2)
            self.ids.humid.text = u'湿度 内 {0} % 外 {1} %'.format(humid_str, humid2_str)
            self.ids.humid.color = text_color
            light = _get_light()
            self.ids.light.text = light if type(light) is str else u'照度 {:.0f} lux'.format(light)
            self.ids.light.color = text_color
            press = _get_pressure()
            self.ids.press.text = press if type(press) is str else u'気圧 {:.1f} hPa'.format(press)
            self.ids.press.color = text_color
            power = _get_power()
            self.ids.power.text = power if type(power) is str else u'電力 {:.1f} W'.format(power)
            self.ids.power.color = text_color
            sleep(30)

    def start(self):
        handle_thread = threading.Thread(target=self.main)
        handle_thread.setDaemon(True)
        handle_thread.start()


class DisplayApp(App):

    def build(self):
        disp = Display()
        disp.start()
        return disp


DisplayApp().run()

display.kv
<Display>:
    BoxLayout:
        temp: temp
        humid: humid
        light: light
        press: press
        power: power
        time: time

        orientation: 'vertical'
 
        Label:
            id: temp
            text: 'Temp'
            font_size: 100

        Label:
            id: humid
            text: 'Humid'
            font_size: 100

        Label:
            id: light
            text: 'Light'
            font_size: 100
            
        Label:
            id: press
            text: 'Press'
            font_size: 100

        Label:
            id: power
            text: 'Power'
            font_size: 100

        Label:
            id: time
            text: 'Time'
            font_size: 110

参考にしたページ
http://takeshid.hatenadiary.jp/entry/2016/06/19/151827
https://torina.top/detail/327/
https://qiita.com/dario_okazaki/items/7892b24fcfa787faface

日本語対応
http://supportdoc.net/support-kivy/03jp.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?