LoginSignup
1

More than 1 year has passed since last update.

posted at

updated at

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?"に。
Screenshot from 2020-07-18 14-15-33.png

ターミナルにて"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()

以下は表示されたウィンドウです。
Screenshot from 2020-07-18 14-21-15.png

考察

そもそも、vscodeのターミナルにおいてはxeyesがxeyes -display :0としなければ表示できませんでした。加えて、通常のターミナルにおいては、特に対策を行わなくてもHello World表示はできていたので、vscodeのターミナル実装に問題があるようです。

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
What you can do with signing up
1