安装pyotherside后导入错误

时间:2017-04-29 12:07:50

标签: qt python-3.x pyotherside

我尝试从ubuntu repos(包pyothersideqml-module-io-thp-pyotherside)以及git repo安装pyotherside

但是在使用Python3导入时,我不断获得ImportError: No module named 'pyotherside'

知道为什么吗?我应该在哪里找到安装pyotheride的路径?

操作系统:Ubuntu 16.04.2

1 个答案:

答案 0 :(得分:0)

PyOtherSide是一个使用QML的python代码的库,反之亦然,在你的项目中你必须使用qml文件和python文件。要运行该程序,您必须执行qml,例如:

{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)
    }
}

您打开终端运行:

qmlscene {your directory}/imageprovider.qml