簡體   English   中英

Vala轉Python,然后再次返回

[英]Vala to Python and back again

我被困在嘗試從Vala / C到Python並再次向下的路徑。 我所有的google-fu都將我帶入了圈子。 我想使用Vala編寫API,然后從Python(或Gnome Javascript)中使用它。

以Clutter為例(這也可能是GTK + 3小部件),這是我的問題:我如何-

去那里

編寫一個自定義Actor,當單擊該Actor時將:

  1. 更改顏色-NB :這是 Vala處理程序中完成的。 即,將vala對象連接到“釋放按鈕”事件。 該處理程序調用一個vala方法:this.set_col('blue');
  2. 是否讓該事件以及一些數據繼續傳遞到Python中-說我想打印“我變成了藍色!” -所以我需要“ blue”作為字符串。

在Python中,我將創建一個Stage,並(通過GI Magic)創建我的新Actor。 我做了所有的Python事情來設置它,然后連接到同一個“按鈕釋放”事件(我想..)

a)將先運行Vala處理程序,然后運行Python? (按順序,或完全沒有。)

b)我是否必須在Vala處理程序中做一些特殊的事情-例如返回true,或者發出一些新信號供Python接收?

再回來

假設該演員被稱為V。我如何:V.set_col('red')(在Python中)並使其運行Vala set_col方法並傳遞一個Python字符串? (我懷疑這在GI下是自動的,但我不確定。)

簡而言之

Vala actor -- event --> handler (in Vala) --> handler (in Python) with data
Vala method <--- method call with args from Python 

我將不勝感激任何鏈接,謝謝。

我知道了。 下面的代碼如下所示:

  1. 在目錄中制作三個文件。
  2. “ build”是一個bash腳本。
  3. “ redsquare.vala”是將成為庫的Vala(.so文件)
  4. “ test.py”是使用該庫的Python3代碼。
  5. 安裝這些:( apt-get bias,對不起)
    • apt-get安裝libgirepository1.0-dev
    • apt-get install gobject-introspection
    • 瓦拉克和公司
  6. 運行./build,它應該研磨伏都教並運行test.py文件。

  7. 您應該看到一個“混亂”窗口。 單擊紅色正方形,然后查看控制台欄。

:-D

HTH。

建立

#!/bin/bash

#
# Script borrowed from Tal Liron at:
# https://github.com/tliron/pygobject-example
#
# I did these to get this script to work:
#
# apt-get install libgirepository1.0-dev
# apt-get install gobject-introspection
#


echo "Cleaning..."

rm -rf tmp
rm -rf lib
rm -rf type
rm -f test

mkdir tmp
mkdir lib
mkdir type

    echo "Building Vala library..."

    # Note 1: Ubuntu package for valac: valac-0.14
    # Note 2: Generates broken gir if --gir= has a directory prefixed to it
    # Note 3: The -X switches for gcc are necessary!
    # Note 4: The generated gir will include GObject-2.0. That gir is
    #         included in Ubuntu package: libgirepository1.0-dev

    valac \
  --pkg clutter-1.0 \
    --library=Palelib \
    --directory=tmp \
    --gir=Palelib-1.0.gir \
    --output=libpalelib.so \
    -X -shared \
    -X -fPIC \
    redsquare.vala

    mv tmp/libpalelib.so lib
    mv tmp/Palelib-1.0.gir type

    # Note: We cannot generate C code and compile in the same call
    #       (We don't need the C code to run the test, but we are curious
    #       as to what Vala is generating. The resulting code will be in
    #       logging.c)
    #valac \
    #--ccode \
    #redsquare.vala


echo "Building typelib..."

# Note 1: Ubuntu package for g-ir-compiler: gobject-introspection
# Note 2: The --shared-library switch is really only necessary when using
#         the gir produced by valac, because it does not include the
#         'shared-library' attribute in <namespace> tag.


g-ir-compiler \
--shared-library=libpalelib.so \
--output=type/Palelib-1.0.typelib \
type/Palelib-1.0.gir

echo "Test Python..."

# Note 1: Ubuntu's default path for typelib files seems to be:
#         /usr/lib/girepository-1.0/.
# Note 2: It is also possible to programmatically change the
#         GI_TYPELIB_PATH environment var in Python (os.environ API).
#         If you do so, make sure to set it before importing from
#         gi.repository.
LD_LIBRARY_PATH=lib \
GI_TYPELIB_PATH=type \
./test.py

redsquare.vala

namespace Palelib {

    public class RedSquare : Clutter.Actor {

    //private vars
    private Clutter.Canvas _canvas;
    private int[] _col = { 255, 0, 0 };

    //Constructor - Needs to be called explicitly from Python by .new()
    public RedSquare() {
      stdout.printf( "RedSquare constructor.\n" );

      _canvas = new Clutter.Canvas();
      _canvas.set_size(300,300);

      this.set_size(300,300);
      this.set_content( _canvas );

      //Connect to the draw signal.
      _canvas.draw.connect(drawme);

      //Make it reactive and connect to the button-press-event
      this.set_reactive(true);
      this.button_press_event.connect( cleek );
    }

    //Button press signal handler
    private bool cleek ( Clutter.ButtonEvent evt ) {
      stdout.printf("Vala cleek() has run!\n");
      this._col = {0,255,0}; //Just change the colour
      this.redraw("from Vala");
      //return true; //Stops the signal here. Python won't get it.
      return false; //Lets the signal carry on going (to Python).
    }

    //Draws the Cairo art to the canvas
    private bool drawme( Cairo.Context ctx, int w, int h) {
      stdout.printf("drawme test.\n");
      ctx.set_source_rgb(this._col[0],this._col[1],this._col[2]);
      ctx.rectangle(0,0,300,300);
      ctx.fill();
      return true;
    }

    //Redraw - forces invalidate which trips the draw event
    //Am gonna call this directly from Python too!
    public void redraw(string? thing) {
      thing = thing ?? "from null"; //tests for null or else
      stdout.printf( "redraw test %s.\n", thing );

      this._canvas.invalidate();
    }
    } //end RedSquare class
} //end namespace

test.py

#!/usr/bin/env python3

"""
Tests the instance of our Vala actor.

I expect to see a red square on the white stage.
(It can be clicked.)

"""

import sys
from gi.repository import Palelib, Clutter

Clutter.init(sys.argv)
stage = Clutter.Stage()
stage.set_size(800, 400)
stage.set_title("Blah blah")
stage.connect('destroy', lambda x: Clutter.main_quit() )


# Make our Object:
rs = Palelib.RedSquare.new() #Note the .new() call. Yuck.
print(rs)
#print(dir(rs)) # See that it is an Actor object.

rs.set_position(100,100)

stage.add_child(rs)

#Force rs to appear. Calls a Vala method and passes a string.
rs.redraw("from Python")

"""
# Crud for testing:
r1 = Clutter.Rectangle()
r1.set_size(50,50)
r1.set_position(0,0)
damnweird = Clutter.Color.new(0,0,0,255)
r1.set_color( damnweird  )

stage.add_child(r1)
"""



"""
Let's get an event going from Python!
Because the RedSquare actor is *already* listening
to a button-press-event (in Vala) this is the second 
such event it will obey. 

I *think* it happens after the vala cleek() method runs.
If you |return true| in cleek(), then this does NOT run,
so that implies that Python is happening second in the chain.
"""
def gogo( a, evt ):
  print ("Hello from gogo. %s %s" % (a,evt))
rs.connect("button_press_event", gogo)



stage.show_all()
Clutter.main()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM