LoginSignup
1
1

More than 3 years have passed since last update.

vscode上wxPython(及びtraitsui)にてDISPLAYエラー

Last updated at Posted at 2020-07-18

環境

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のターミナル実装に問題があるようです。

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