Gtk 4.10 にこんな class が追加されていた。
いやえっと、ファイルからアプリを起動するなら gio コマンドでよくね?
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 | #!/usr/bin/env python3 import gi, subprocess gi.require_version( 'Gtk' , '4.0' ) gi.require_version( 'Adw' , '1' ) from gi.repository import Gtk, Adw, Gio PICFILE = './test.jpg' class Win(Gtk.ApplicationWindow): ''' Default App Launcher: Sample Code ''' def __init__( self , a): # Set Adwaita Style manager = Adw.StyleManager.get_default() manager.set_color_scheme(Adw.ColorScheme.DEFAULT) # init Gtk.ApplicationWindow.__init__( self , application = a) # button button = Gtk.Button(label = 'Launch test.jpg' ) button.connect( 'clicked' , self .on_button_clicked) self .set_child(button) def on_button_clicked( self , button): subprocess.run([ 'gio' , 'open' , PICFILE]) app = Gtk.Application() app.connect( 'activate' , lambda a: Win(a).present()) app.run() |
みたいに。
GtkUriLauncher もある、gio コマンドは URI でもイケるんだが。
とはいえ、わざわざ追加したということは何かあるんだろうなって。
とっとと試してみる、使い方は GtkFileDialog と同じみたいね。
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 | #!/usr/bin/env python3 import gi gi.require_version( 'Gtk' , '4.0' ) gi.require_version( 'Adw' , '1' ) from gi.repository import Gtk, Adw, Gio PICFILE = './test.jpg' class Win(Gtk.ApplicationWindow): ''' GtkFileLauncher: Sample Code ''' def __init__( self , a): # Set Adwaita Style manager = Adw.StyleManager.get_default() manager.set_color_scheme(Adw.ColorScheme.DEFAULT) # init Gtk.ApplicationWindow.__init__( self , application = a) # button button = Gtk.Button(label = 'Launch test.jpg' ) button.connect( 'clicked' , self .on_button_clicked) self .set_child(button) def on_button_clicked( self , button): f = Gio.File.new_for_path(PICFILE) # GtkFileLauncher launcher = Gtk.FileLauncher( file = f) launcher.launch( self , None , self .on_launch_finish) def on_launch_finish( self , launcher, res): try : result = launcher.launch_finish(res) if result: print ( 'Success' ) else : print ( 'Failed' ) except Exception as e: print (f 'Launch Error: {e}' ) app = Gtk.Application() app.connect( 'activate' , lambda a: Win(a).present()) app.run() |
そういうことか!
デフォルトアプリではなく何で開くかを選択できる、ということだった。
てか GtkAppChooserDialog の後継でした、名前がまぎらわしいわ!
デフォルトアプリ起動なら上のコードほうが簡単です、と一言。
ということで。
今日が囀り続けるキビタキを三十分追いかけ、とうとう一枚も撮れず。
ヤケクソで午後はキジを探したら、ものすごく遠くにしか出てこないし。
あきらめて帰ろうと思ったところにヒバリが現れてくれました。
ヒバリのトサカって正面から見るとこんな感じなんだね。