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.

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

Posted at

ラズパイにディスプレイをくっつけてセンサーの値を表示しようとした。
当初KIVYを選定していたが、Raspbean OS Version upをした際に動作しなくなったため、Tkinterに乗り換えた。

ただ表示するだけでなく、一定間隔で表示を更新したかったため、threadingも使用。
また、夜部屋の灯りを消したときに煌々と光られると困るため、時間帯による色調整も実施。
ラズパイ起動時にcrontabから起動したかったため、ディスプレイ指定も行う必要があった。os.environ['DISPLAY'] = ':0'

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

import tkinter as tk
import tkinter.font as f
import threading
import os
import sensors
from datetime import datetime

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

class GUI(tk.Frame):
    def __init__(self,master = None):
        super().__init__(master)
        self.master = master
        master.geometry("480x350")
        # master.configure(bg="black")
        master.tk_setPalette(background="black")
        master.title("Test")
        font1 = f.Font(size='35')

        self.test = tk.Frame(self.master)
        self.test.pack()
        self.labelTemp1 = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelTemp1.pack()
        self.labelHumid1 = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelHumid1.pack()
        self.labelPress = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelPress.pack()
        self.labelLight = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelLight.pack()
        self.labelTemp2 = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelTemp2.pack()
        self.labelHumid2 = tk.Label(self.test, fg='white', bg='black', font=font1)
        self.labelHumid2.pack()

        self.timeEvent()# タイマー起動

    # タイマー起動用関数
    def timeEvent(self):
        th = threading.Thread(target=self.update)
        th.start()
        self.after(60000, self.timeEvent)

    # スレッド処理実体
    def update(self):
        data = _get_temperature()
        # print(data)
        self.labelTemp1["text"] = '気温 ' + str(data) + ''
        data = _get_humid()
        # print(data)
        self.labelHumid1["text"] = '湿度 ' + str(data) + ' %'
        data = _get_pressure()
        # print(data)
        self.labelPress["text"] = '気圧 ' + str(data) + ' hPa'
        data = _get_light()
        # print(data)
        self.labelLight["text"] = '照度 ' + str(data) + ' level'
        data = _get_temperature2()
        # print(data)
        self.labelTemp2["text"] = '気温2 ' + str(data) + ''
        data = _get_humid2()
        # print(data)
        self.labelHumid2["text"] = '湿度2 ' + str(data) + ' %'
        hour = datetime.now().hour
        if hour > 23 or hour < 7:
            self.labelTemp1["fg"] = 'gray'
            self.labelHumid1["fg"] = 'gray'
            self.labelPress["fg"] = 'gray'
            self.labelLight["fg"] = 'gray'
            self.labelTemp2["fg"] = 'gray'
            self.labelHumid2["fg"] = 'gray'
        else:
            self.labelTemp1["fg"] = 'white'
            self.labelHumid1["fg"] = 'white'
            self.labelPress["fg"] = 'white'
            self.labelLight["fg"] = 'white'
            self.labelTemp2["fg"] = 'white'
            self.labelHumid2["fg"] = 'white'

if __name__ == "__main__":
    os.environ['DISPLAY'] = ':0'
    gui = tk.Tk()
    app = GUI(master = gui)
    gui.attributes('-fullscreen', True)
    app.mainloop()

0
0
1

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?