GUIから外部ソフトを実行したい
以前に、GUIからPDFファイルを開きたい - 理想のユーザ・インターフェイスを求めてを検討したが、コマンドラインから実行できる任意のプログラムを、同じようにGUIボタンを押したときに実行できるようにする。
やりかたはPDFファイルを開いたときとほぼ同じように、Process.Startで実行することができる。引数を指定するときには、コンマで区切る(Process.Start(command, arg))。
サンプルコードは以下の通り。ボタンを押したときに、Notepad++を起動する。
import clr clr.AddReference('System.Windows.Forms') clr.AddReference('System.Drawing') clr.AddReference('System') import System.Drawing import System.Windows.Forms import System.Diagnostics from System.Drawing import * from System.Windows.Forms import * from System.Diagnostics import Process from System.Windows.Forms import Application class MainForm(Form): def __init__(self): self.InitializeComponent() def InitializeComponent(self): self._button1 = System.Windows.Forms.Button() self.SuspendLayout() # # button1 # self._button1.Location = System.Drawing.Point(53, 83) self._button1.Name = "button1" self._button1.Size = System.Drawing.Size(175, 56) self._button1.TabIndex = 0 self._button1.Text = "button1" self._button1.UseVisualStyleBackColor = True self._button1.Click += self.Button1Click # # MainForm # self.ClientSize = System.Drawing.Size(284, 262) self.Controls.Add(self._button1) self.Name = "MainForm" self.Text = "test4" self.ResumeLayout(False) def Button1Click(self, sender, e): filename = "C:/Program Files (x86)/Notepad++/notepad++.exe" Process.Start(filename); Application.EnableVisualStyles() form = MainForm() Application.Run(form)