We have moved to a new Sailfish OS Forum. Please start new discussions there.
3

I can't get pyotherside right [answered]

asked 2014-02-09 08:51:31 +0200

triztan79 gravatar image

updated 2014-02-09 10:16:05 +0200

I can't get even the simpliest application to work with Sailfish and PyOtherSide. What i'm doing wrong? Is it with addImportPath? Here is the code:

pressbutton.qml

import QtQuick 2.0
import Sailfish.Silica 1.0
import io.thp.pyotherside 1.0

ApplicationWindow
{
    initialPage: Page {
        id: page

        Button {
            id: mybtn
            text: "press"
            anchors.centerIn: page
            onClicked: {
                py.call('pressbutton.pressme', function() {});
            }
        }

        Python {
            id: py
            Component.onCompleted: {
                addImportPath(Qt.resolvedUrl('/usr/share/pressbutton/qml'));
                importModule('pressbutton', function() {});
            }
        }
    }

    cover: Qt.resolvedUrl("cover/CoverPage.qml")
}

and pressbutton.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pyotherside

def pressme():
    f = open("/home/nemo/Documents/file.txt","a")
    f.write("this is example text")
    f.close()
edit retag flag offensive reopen delete

The question has been closed for the following reason "the question is answered, an answer was accepted" by foss4ever
close date 2014-02-10 14:44:40.262693

Comments

What does the QtCreator / SDK say when you try to build / deploy that?

foss4ever ( 2014-02-09 10:48:53 +0200 )edit

I deploy that with "mb2 -t SailfishOS-armv7hl rpm" and it doesn't give any error. The App works (there's button in the middle of screen) but when pressed it doesn't create new file to the Documents-folder.

triztan79 ( 2014-02-09 12:40:05 +0200 )edit

You should improve your example code with onError callback and return a value from your pythons module method that can be printed out with console() QML function. Do also remember that you call your python code async. For a synchronous test use call_sync(). HTH.

Nekron ( 2014-02-09 16:31:36 +0200 )edit

1 Answer

Sort by » oldest newest most voted
5

answered 2014-02-09 18:22:47 +0200

thp gravatar image

Looking at your code:

py.call('pressbutton.pressme', function() {});

Here, you are missing the args parameter of the call, it should be:

py.call('pressbutton.pressme', [], function(result) {});

(note the addition of [] - an empty argument list - there)

Additionally, if you don't have PyOtherSide 1.1 (e.g. from Nemo Mobile "devel" repositories), you have to strip the leading 'file://' from the Qt.resolvedUrl() result for now (PyOtherSide 1.1 and later will strip it for you automatically, but 1.1 is not yet in the Sailfish OS release repositories, it should be with one of the next updates), if the import path should be relative to the QML file:

addImportPath(Qt.resolvedUrl('.').substr('file://'.length));

If you are just adding an absolute, hardcoded path, you can just use that, without Qt.resolvedUrl():

addImportPath('/usr/share/pressbutton/qml');

That should make your example work.

By the way, the Python function can be rewritten as:

def pressme():
    with open("/home/nemo/Documents/file.txt","a") as f:
        f.write("this is example text")

See this article for how "with" is used there. If you want to write line-wise to that file, use f.write('this is example text\n') or even print with the file= keyword argument, like:

def pressme():
    with open("/home/nemo/Documents/file.txt","a") as f:
        print("this is example text", file=f)

Let me know if that works for you and if you have any other questions :)

edit flag offensive delete publish link more

Comments

Now it works! thanks! That is a great job you do with PyOtherSide.

triztan79 ( 2014-02-09 18:42:44 +0200 )edit

Question tools

Follow
1 follower

Stats

Asked: 2014-02-09 08:51:31 +0200

Seen: 1,178 times

Last updated: Feb 09 '14