Python で簡単な「プログラム ランチャー」を作成しました。現時点では、タブ区切りのテキスト ファイルがあります。
メモ帳 c:\windows\notepad.exe
書き込み c:\windows\write.exe
プログラムはテキストファイルを読み取り、オブジェクトの配列を作成します。各オブジェクトには、name プロパティ (メモ帳など) とルート プロパティ (C:\windows\notepad.exe など) があります。次に、オブジェクトごとに、ボタンに正しい名前でボタンを作成し、ボタンをクリックすると、ルートを使用して正しいプログラムを実行する必要があります。
プログラムはほとんど動作します。実際、オブジェクトの配列は正しく形成されています。これは、for ループが 2 つの異なるプログラム名と 2 つの異なるルートを正しく出力するためです。問題は、両方のボタンが正しくラベル付けされていても、書き込みプログラムを起動することです! 問題はコールバックのどこかで発生していると思いますが、私の Python の知識はこれを解決するのに十分ではありません! 以下のコードからわかるように、「インライン」コールバックを試し、「runprog」関数を定義しました。どちらも同じ結果になります。
あなたの助けをいただければ幸いです。
import Tkinter as tk
import subprocess
class MyClass:
def __init__(self, thename,theroute):
self.thename=thename
self.theroute=theroute
myprogs = []
myfile = open('progs.txt', 'r')
for line in myfile:
segmentedLine = line.split("\t")
myprogs.append(MyClass(segmentedLine[0],segmentedLine[1]))
myfile.close()
def runprog(progroute):
print(progroute)
subprocess.call([progroute])
root = tk.Tk()
button_list=[]
for prog in myprogs:
print(prog.thename)
print(prog.theroute)
button_list.append(tk.Button(root, text=prog.thename, bg='red', command=lambda: runprog(prog.theroute)))
# button_list.append(tk.Button(root, text=prog.thename, bg='red', command= lambda: subprocess.call(prog.theroute)))
# show buttons
for button in button_list:
button.pack(side='left', padx=10)
root.mainloop()