PyQt5 Could not connect to display

2019-08-21 17:46发布

问题:

to learn python I decided to make a small GUI that will interact with a web API. So without much thinking I created a virtual machine with Vagrant (ubuntu/trusty64) and installed PyQt5. I made a small script to create a window (here it is)

import sys
from PyQt5 import QtWidgets

app = QtWidgets.QApplication(sys.argv)

main_window = QtWidgets.QMainWindow()
main_window.show()

sys.exit(app.exec_())

When I executed my script I ended up with this error

QXcbConnection: Could not connect to display

So I guess the problem comes from the fact that I'm using a virtual-machine without a display. Isn't there a way to make it use my Windows display just to render my program? Or do I need to install a VM with virtualbox?

Thanks for your time.

回答1:

When provisioning your VM with Vagrant, you may specify to boot to GUI, such as:

config.vm.provider "virtualbox" do |v|
  v.gui = true
end

More information here

This will then launch the Qt app, using the VM's display.