PyGObject が Fedora 34 では例外になってもプロセスが続くようになった。
GTK4 だからかと思ったら GTK3 でやっても同じだった。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #!/usr/bin/env python3 import gi, sys gi.require_version( 'Gtk' , '3.0' ) from gi.repository import Gtk class TestWindow(Gtk.ApplicationWindow): def __init__( self , app): Gtk.ApplicationWindow.__init__( self , application = app) try : btn = Gtk.Button(label = 'SUZUKI' ) btn.connect( 'clicked' , self .on_button_clicked) self .label = Gtk.Label(label = 'Motor Cycle' ) box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL) self .add(box) box.pack_start(btn, False , False , 0 ) box.pack_start( self .label, False , False , 0 ) # err box.pack_start(null_widget, False , False , 0 ) except Exception as e: print (f '@@@Error@@@@: ${e}' , file = sys.stderr) def on_button_clicked( self , widget): # err self .label.null_func() class TestApplication(Gtk.Application): def __init__( self ): Gtk.Application.__init__( self , application_id = 'org.suzuki.sv' ) def do_activate( self ): w = TestWindow( self ) w.show_all() app = TestApplication() app.run() |
ところで前回は例外を受け取っても pass するコードだった。
これだと何も出力されなくなってしまうね。
stderr に出力するようにしたらアプリを実行中でも表示される。
Gedit の外部ツールなら色が着くというおまけ付き。
この仕様変更は gir 側なのか PyGObject 独自なのか。
ということで Gjs で同じことをやってみる。
いつのまにかこんなサイトができていたのね。
ガイドを見るとまだ GTK3 での解説だけどね。
Legacy Class Syntax | GNOME Javascript
ES6 以降でクラスにする方法が筆者がよく使っている方法に。
コレだ!って解説が無かったから動いたのを書いていただけですが。
こういうサイトがこう書いてくれると以後安心して書けますね。
ただ、PyGObject と比べるとちょっと面倒臭いよね。
ただでさえ let と new とセミコロンとブレースが面倒なのに。
それはいいとして、とっとと上記を GTK4 で Gjs に書き換え。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #!/usr/bin/gjs imports .gi.versions.Gtk = '4.0' ; const {GObject, Gtk} = imports .gi; var TestWindow = GObject.registerClass({ GTypeName: 'TestWindow' }, class TestWindow extends Gtk.ApplicationWindow { _init(app) { super ._init({ application: app }); try { let btn = new Gtk.Button({label: 'SUZUKI' }); btn.connect( 'clicked' , ()=> { // err this .label.null_func(); }); this .label = new Gtk.Label({label: 'Motor Cycle' }); let box = new Gtk.Box({orientation: Gtk.Orientation.VERTICAL}); this .set_child(box); box.append(btn); box.append( this .label); // err box.append(null_widget); } catch (e) { printerr( `@@@Error@@@@: ${e.message}` ); } } }); var TestApplication = GObject.registerClass({ GTypeName: 'TestApplication' }, class TestApplication extends Gtk.Application { _init() { super ._init({ application_id: 'org.suzuki.bandit' }); } vfunc_activate() { let w = new TestWindow( this ); w.present(); } }); let app = new TestApplication(); app.run( null ); |
うん完全に同じだ、gir 側の仕様変更だったようです。
プロセスの終了を待たずに printf debug ができるのは嬉しいね。