Import Error after installing pyotherside

本小妞迷上赌 提交于 2020-01-17 06:29:44

问题


I tried installing pyotherside from the ubuntu repos (packages qml-module-io-thp-pyotherside and pyotherside) as well as from the git repo.

But in when importing it in Python3 I keep getting ImportError: No module named 'pyotherside'.

Any knows why? What is the path where I should find pyotherside installed?

OS: Ubuntu 16.04.2


回答1:


PyOtherSide is a library that uses the python code from QML and not vice versa, in your project you must the qml file and the python file. To run the program you must execute the qml, for example:

{your directory}
├── imageprovider.py
└── imageprovider.qml

imageprovider.py

import pyotherside
import math

def render(image_id, requested_size):
    print('image_id: "{image_id}", size: {requested_size}'.format(**locals()))

    # width and height will be -1 if not set in QML
    if requested_size == (-1, -1):
        requested_size = (300, 300)

    width, height = requested_size

    # center for circle
    cx, cy = width/2, 10

    pixels = []
    for y in range(height):
        for x in range(width):
            pixels.extend(reversed([
                255, # alpha
                int(10 + 10 * ((x - y * 0.5) % 20)), # red
                20 + 10 * (y % 20), # green
                int(255 * abs(math.sin(0.3*math.sqrt((cx-x)**2 + (cy-y)**2)))) # blue
            ]))
    return bytearray(pixels), (width, height), pyotherside.format_argb32

pyotherside.set_image_provider(render)

imageprovider.qml

import QtQuick 2.0
import io.thp.pyotherside 1.5

Image {
    id: image
    width: 300
    height: 300

    Python {
        Component.onCompleted: {
            // Add the directory of this .qml file to the search path
            addImportPath(Qt.resolvedUrl('.'));

            importModule('imageprovider', function () {
                image.source = 'image://python/image-id-passed-from-qml';
            });
        }

        onError: console.log('Python error: ' + traceback)
    }
}

You open a terminal run:

qmlscene {your directory}/imageprovider.qml


来源:https://stackoverflow.com/questions/43695528/import-error-after-installing-pyotherside

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!