Gtk(Gjs) Tips
# 最終更新日 2023.11.19
GtkWindow
正直 GUI を作るなら PyGObject のほうが圧倒的に簡単 GTK4(Python) Tips ということで基本だけ書いておきます
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | #!/usr/bin/gjs -m /** * 複数のバージョンがある場合は指定が必要 */ /** * 必要な Gir をインポートする */ import System from 'system' ; /** * ウインドウクラスの登録、言語の class という意味では無い * GObject.registerClass は何も考えずこう書く * 継承は必ず var 宣言で行う必要がある */ var MyWindow = GObject.registerClass({ GTypeName: 'MyWindow' }, class MyWindow extends Gtk.ApplicationWindow { /** * 初期化は constructor ではなく _init を使う */ _init(app) { /** * 継承の場合は親クラスの _init を呼ぶ * _init や new の引数はプロパティをオブジェクト形式で指定 */ super ._init({ application: app, title: 'はろー Gjs!' }); /** * 変数は基本的に let 宣言で行う * 後で呼び出す場合は this.button と this にくっつける */ let button = new Gtk.Button({ label: 'ボタンを押してみてね(_B)' , use_underline: true }); /** * シグナルハンドラを無名関数で行う場合はアロー関数にする * function() で書くと this が呼び出し元に変わってしまう */ button.connect( 'clicked' , (widget)=> { if (widget.label == 'ハローワールド!' ) this .title = 'ハローワールド!' else widget.label = 'ハローワールド!' ; }); /** * add は set_child になりました、child プロパティに代入でもいい */ this .child = button; /** * Widget はデフォルトが「表示」になり show() は廃止されました * 非表示にしたい Widget がある場合には visible プロパティにて */ //this.show_all(); } }); /** * アプリケーションクラスの作成、Gtk.main 関数は廃止されたので必ず必要 * メインループを受け持ち管理するウインドウが無くなると終了します */ var MyApplication = GObject.registerClass({ GTypeName: 'MyApplication' }, class MyApplication extends Gtk.Application { _init() { super ._init(); } /** * オーバーライドは * [ vfunk_メソッド名 ] * で予約されているので上書き注意 */ vfunc_activate() { let window = new MyWindow( this ); } }); /** * 実行、argv の指定はこうする */ let app = new MyApplication(); app.run([System.programInvocationName].concat(ARGV)); |
Copyright(C) sasakima-nao All rights reserved 2002 --- 2025.