-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 34ea420
Showing
30 changed files
with
1,790 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
PyOtherSide: Asynchronous Python 3 Bindings for Qt 5 | ||
Copyright (c) 2011, 2013, Thomas Perl <m@thp.io> | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
PyOtherSide: Asynchronous Python 3 Bindings for Qt 5 | ||
==================================================== | ||
|
||
This is a QML Plugin that provides access to a Python 3 interpreter from QML. | ||
See examples/ for some basic code examples what you can do with this. | ||
|
||
Requirements: | ||
|
||
Qt >= 5.0.2 | ||
Python >= 3.3.0 | ||
|
||
|
||
To build and install the QML plugin: | ||
|
||
qmake | ||
make | ||
make install | ||
|
||
|
||
To run the included unit tests: | ||
|
||
qmake | ||
make | ||
tests/tests | ||
|
||
|
||
To run one of the included examples (after installing the plugin): | ||
|
||
qmlscene examples/simple/simple.qml | ||
qmlscene examples/events/main.qml | ||
qmlscene examples/atexit/main.qml | ||
qmlscene examples/notes/main.qml | ||
|
||
|
||
Website and Git repository: | ||
|
||
http://thp.io/2011/pyotherside/ | ||
http://github.com/thp/pyotherside/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This examples shows how to do cleanups and other things when | ||
the application exists by using pyside.atexit(). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import pyotherside | ||
|
||
def called_when_exiting(): | ||
print('Now exiting the application...') | ||
|
||
pyotherside.atexit(called_when_exiting) | ||
print('python loaded') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
import QtQuick 2.0 | ||
import io.thp.pyotherside 1.0 | ||
|
||
Rectangle { | ||
width: 200 | ||
height: 200 | ||
color: 'red' | ||
|
||
Python { | ||
id: python | ||
Component.onCompleted: { | ||
addImportPath('examples/atexit'); | ||
importModule_sync('main'); | ||
} | ||
|
||
onError: console.log('error in python: ' + traceback); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This example demonstrates the use of pyotherside.send() to send events to Qt. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
import pyotherside | ||
|
||
import threading | ||
import time | ||
|
||
COLORS = ['red', 'green', 'blue'] | ||
|
||
def thread_func(): | ||
i = 0 | ||
while True: | ||
pyotherside.send('append', 'Next Number: ', i) | ||
if i % 2 == 0: | ||
color = COLORS[int((i / 2) % len(COLORS))] | ||
pyotherside.send('color', color) | ||
i += 1 | ||
time.sleep(1) | ||
|
||
thread = threading.Thread(target=thread_func) | ||
thread.start() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
import QtQuick 2.0 | ||
import io.thp.pyotherside 1.0 | ||
|
||
Rectangle { | ||
width: 400 | ||
height: width | ||
|
||
Python { | ||
id: py | ||
|
||
Component.onCompleted: { | ||
// Add the Python library directory to the import path | ||
addImportPath('examples/events'); | ||
|
||
// Import the main module | ||
importModule('main', function () { | ||
console.log('Main module is now imported'); | ||
}); | ||
} | ||
|
||
onReceived: { | ||
var command = data[0], args = data.splice(1); | ||
|
||
if (command == 'append') { | ||
output.text += '\n' + args; | ||
} else if (command == 'color') { | ||
output.color = args[0]; | ||
} | ||
} | ||
} | ||
|
||
Text { | ||
id: output | ||
anchors.centerIn: parent | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
A very simple notetaking application that uses Python to load | ||
and save a string in a text file in the user's home directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
import os | ||
import threading | ||
import time | ||
|
||
import pyotherside | ||
|
||
class Notes: | ||
def __init__(self): | ||
self.filename = os.path.expanduser('~/pyotherside_notes.txt') | ||
self.thread = None | ||
self.new_text = self.get_contents() | ||
|
||
def save_now(self): | ||
print('Saving file right away at exit') | ||
self._update_file(now=True) | ||
|
||
def _update_file(self, now=False): | ||
if not now: | ||
time.sleep(3) | ||
print('Saving file now') | ||
open(self.filename, 'w').write(self.new_text) | ||
self.thread = None | ||
|
||
def get_contents(self): | ||
if os.path.exists(self.filename): | ||
return open(self.filename).read() | ||
else: | ||
return '<new file>' | ||
|
||
def set_contents(self, text): | ||
self.new_text = text | ||
if self.thread is None: | ||
print('Scheduling saving of file') | ||
self.thread = threading.Thread(target=self._update_file) | ||
self.thread.start() | ||
|
||
notes = Notes() | ||
pyotherside.atexit(notes.save_now) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
import QtQuick 2.0 | ||
import io.thp.pyotherside 1.0 | ||
|
||
Rectangle { | ||
width: 300 | ||
height: 200 | ||
|
||
TextInput { | ||
id: ti | ||
|
||
anchors.fill: parent | ||
|
||
onTextChanged: { | ||
py.call('main.notes.set_contents', [text], function() { | ||
console.log('Changes sent to Python'); | ||
}); | ||
} | ||
|
||
Python { | ||
id: py | ||
Component.onCompleted: { | ||
addImportPath('examples/notes'); | ||
importModule('main', function () { | ||
console.log('imported python module'); | ||
call('main.notes.get_contents', [], function(result) { | ||
console.log('got contents from Python: ' + result); | ||
ti.text = result; | ||
}); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Just some simple API usage examples for PyOtherSide. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import QtQuick 2.0 | ||
import io.thp.pyotherside 1.0 | ||
|
||
Rectangle { | ||
width: 200 | ||
height: 200 | ||
color: 'blue' | ||
|
||
ListView { | ||
id: listView | ||
anchors.fill: parent | ||
delegate: Text { color: 'white'; text: modelData } | ||
} | ||
|
||
Python { | ||
id: python | ||
|
||
Component.onCompleted: { | ||
// Asynchronous module importing | ||
importModule('os', function() { | ||
console.log('Python module "os" is now imported'); | ||
|
||
// Asynchronous function calls | ||
call('os.listdir', [], function(result) { | ||
console.log('dir listing: ' + result); | ||
listView.model = result; | ||
}); | ||
|
||
// Synchronous calls - avoid these, they block the UI | ||
call_sync('os.chdir', ['/']); | ||
console.log('files in /: ' + call_sync('os.listdir', ['.'])); | ||
}); | ||
|
||
// sychronous imports and calls - again, avoid! | ||
importModule_sync('pyotherside'); | ||
call_sync('pyotherside.send', ['hello world!', 123]); | ||
|
||
// error handling | ||
importModule_sync('thismoduledoesnotexisthopefully'); | ||
evaluate('[ 123 [.syntax234-error!'); | ||
} | ||
|
||
onError: { | ||
// when an exception is raised, this error handler will be called | ||
console.log('python error: ' + traceback); | ||
} | ||
|
||
onReceived: { | ||
// asychronous messages from Python arrive here | ||
// in Python, this can be accomplished via pyotherside.send() | ||
console.log('got message from python: ' + data); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
TEMPLATE = subdirs | ||
SUBDIRS += src tests | ||
CONFIG += ordered |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Link against Python 3.3 | ||
PYTHON_CONFIG = python3.3-config | ||
QMAKE_LIBS += $$system($$PYTHON_CONFIG --ldflags) | ||
QMAKE_CXXFLAGS += $$system($$PYTHON_CONFIG --includes) |
Oops, something went wrong.