python3 tkui.py 直接執行可以..但是無法在開機時自動執行, 原因是執行X Window應用程式(X X Client),若要能連接到X Server 時, X Client 必須要提供兩個設置
- 設置遠端 XServer 的 DISPLAY 環境變數。
export DISPLAY=:0.0 - 能讀到 Xauthority下的cookie檔 ( 位在每個user 的HOME目錄的.Xauthority 目錄)
export XAUTHORITY=/home/<YOUR-USER-NAME>/.Xauthority
通常用自己登入帳號去執行X Client 程式沒有問題, 但若不是或經由 sudo 去執行,則必須設定 Xauthority的位置.
用pi 帳戶, 執行 python3 tkui.py 沒有問, 但是用 sudo python3 tkui.py 會出現錯誤
例如:
pi@raspberrypi:~ $ sudo python3 tkui.py
Invalid MIT-MAGIC-COOKIE-1 keyInvalid MIT-MAGIC-COOKIE-1 keyTraceback (most recent call last):
File "tkui.py", line 16, in <module>
win=Tk.Tk()
File "/usr/lib/python3.5/tkinter/__init__.py", line 1880, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display ":0.0"
解決方式:
pi@raspberrypi:~ $ export XAUTHORITY=/home/pi/.Xauthoritypi@raspberrypi:~ $ sudo python3 tkui.py ==> 能正常執行,因為能找到Xauthority 檔案
References:
https://iwf1.com/quick-fix-invalid-mit-magic-cookie-1-sudo
Thanks for sharing, nice post! Post really provice useful information!
回覆刪除Giaonhan247 chuyên dịch vụ mua hàng mỹ cũng như mua hàng amazon và dịch vụ pandora úc cũng như hướng dẫn cách order hàng mỹ hay mua hàng trên ebay việt nam ship về VN uy tín.