Windows 7 を Linux の GNOME のように便利に使いたい、その2。
Windows でスクリプトを使うのに Linux に比べ何が不便なのか。
書くまでも無い、実行パーミッションという概念が無いこと。
すべてが拡張子による関連付けで決まってしまうこと。
特に普通の Python はインストールで *.py に関連付けられるので悲惨。
ファイルをうっかり W クリックすると無意味に cmd.exe コンソールが出る。
ま、インストールする気は無いけど、Windows では何の約にも立たない。
Windows 版 Python はその辺りを考えてか Pythonw という exe が同根されている。
コンソール不要なら拡張子を *.pyw にすればいい、だけどそれだけだ。
何より Windows ではコンソールなんてほとんどの人が使わない。
IronPython にも同様に ipyw(64) という exe が同根されている。
しかしよく考えてみれば私の Y901x や SeeMe も実行パーミッションは付けていない。
同根のシェルスクリプトからメインファイルを Python に引渡しているだけ。
ならば ipyw64.exe を利用して同じようにすればいいんでないの?
ということで考えた。
*.py をテキストエディタに関連付け
*.pyw を ipyw64.exe に関連付け(私の Windows 7 が 64bit なので)
というくだらない方法だがこれで Linux で Python を使っている時と同様に使える。
実行はシェルスクリプトの変わりに ipyw64.exe にやってもらおうという考え。
main_window.py
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 | # -*- coding: UTF-8 -*- import clr clr.AddReferenceByPartialName( "PresentationCore" ) clr.AddReferenceByPartialName( "PresentationFramework" ) clr.AddReferenceByPartialName( "WindowsBase" ) from System import * from System.Windows import * from System.Windows.Controls import * from System.Windows.Media.Imaging import * class PicViewWindow(Window): def __init__( self ): self .Title = "PicView" self .Drop + = self .on_drop self .AllowDrop = True self .Width = 300 self .Height = 300 self .image = Image() self .Content = self .image def on_drop( self , sender, e): filename = e.Data.GetData(DataFormats.FileDrop)[ 0 ].ToString() self .image.Source = BitmapImage(Uri(filename)) def main(): Application().Run(PicViewWindow()) if __name__ = = "__main__" : main() |
picview.pyw
1 2 | import main_window main_window.main() |
という2つのファイルを用意し上記関連付けを行っておく。
picview.pyw を W クリックしてみる。
ということができる。
main() という関数を用意するのがミソ。
debug 時は EmEditor 等テキストエディタの機能で以下のように実行できるから。
ぱぇぽぃ2 ? Blog Archive ? EmEditor を Gedit 風にカスタマイズ
これでコードを編集したい時は *.py 実行時は *.pyw を W クリックという環境完成。
アイコンが違うので解りやすいしこのほうがコード分割も行いやすい。
ただ OS デフォルト動作では無いので配布に関してはどうするかだ…