1
2

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 5 years have passed since last update.

Raspberry pi 3+にsshからメッセージを表示する

Last updated at Posted at 2019-06-05

<<<小技ネタ>>>

sshから実行されるアプリからHDMI出力のMessageBoxを表示する。

まず、UIを取り扱うためにtkinterをインストールする。

$ sudo apt-get install python3-tk

動作チェック

まず、GUIから実行テスト

$ python -m tkinter

image.png

以下が表示されれば成功

image.png
sshターミナルからコマンドを打ち込むと

$ python -m tkinter
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/tkinter/__main__.py", line 7, in <module>
    main()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 3988, in _test
    root = Tk()
  File "/usr/local/lib/python3.6/tkinter/__init__.py", line 2023, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ""

couldn't connect to displayと怒られてしまう。

sshは、CUIだから当たり前だ!

HDMIモニターに表示するにはどうしたらよいだろうか?

$ DISPLAY=:0 python -m tkinter

ディスプレイを指定するとHDMIに表示できる。

image.png

サンプルアプリを紹介しよう。

alert.py
# !/usr/local/bin/python3.6
# -*- coding:utf-8 -*-
import tkinter as tk,os,sys
import tkinter.font as font
os.environ['DISPLAY']=':0' #ディスプレイの出力先
class Message(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        w=tk.Message(root,text=Msg,font=("", 60, "bold"))
        w.pack(expand=1, fill="both")
if __name__ == '__main__':
    root = tk.Tk()
    root.title(sys.argv[1])
    root.attributes("-zoomed", "1")
    Msg=sys.argv[2]
    app = Message(root)
    while True:      #イベントループ
        try:
            root.update()
        except KeyboardInterrupt:
            break
    sys.exit()

実行させてみよう

実行権を付加する
$ sudo chmod 777 alert.py

./alert.py Alert CPU高温注意

image.png

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?