vscode上wxPython(及びtraitsui)にてDISPLAYエラー
環境
Ubuntu18.04
wxPython 4.0.4
Python 3.7
経緯
traitsuiのHello worldが動かなかったので、そもそもtoolkitとして使用しているwxPythonが動くのか以下のHello Worldコードで確認しました。
# First things, first. Import the wxPython package.
import os
import wx
# Next, create an application object.
app = wx.App()
print(app.IsDisplayAvailable())
# Then a frame.
frm = wx.Frame(None, title="Hello World")
# Show it.
frm.Show()
# Start the event loop.
app.MainLoop()
すると以下のようなエラー"Unable to access the X Display, is $DISPLAY set properly?"に。
ターミナルにて"DISPLAY=:0"とか打ったり、launch.jsonにてenv変数にてDISPLAYを設定してもダメでした。
解法
結局、以下の様にコード上で環境変数指定して解決できました。
# First things, first. Import the wxPython package.
import os
import wx
os.environ["DISPLAY"] = ":0"
# Next, create an application object.
app = wx.App()
print(app.IsDisplayAvailable())
# Then a frame.
frm = wx.Frame(None, title="Hello World")
# Show it.
frm.Show()
# Start the event loop.
app.MainLoop()
考察
そもそも、vscodeのターミナルにおいてはxeyesがxeyes -display :0
としなければ表示できませんでした。加えて、通常のターミナルにおいては、特に対策を行わなくてもHello World表示はできていたので、vscodeのターミナル実装に問題があるようです。
Author And Source
この問題について(vscode上wxPython(及びtraitsui)にてDISPLAYエラー), 我々は、より多くの情報をここで見つけました https://qiita.com/bunnyhopper_isolated/items/8457471fb3fffaf3ed7c著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .