任意のファイルを選択させるダイアログを表示するプログラムを作成しました。
これでファイルの指定をプログラム直書きしなくて済みそうです。
以下のソースです。 tkinter モジュールを利用して作成しました。
解説は後日行いたいと思います。
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 |
# coding: cp932 import os,sys from tkinter import * from tkinter import ttk from tkinter import filedialog from tkinter import messagebox # import [ファイル選択して処理をしたいpythonファイルを指定] # 参照ボタンのイベント # button1クリック時の処理 def button1_clicked(): fTyp = [("","*")] iDir = os.path.abspath(os.path.dirname(__file__)) filepath = filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir) file1.set(filepath) # 参照2ボタンのイベント # button4クリック時の処理 def button4_clicked(): fTyp = [("","*")] iDir = os.path.abspath(os.path.dirname(__file__)) filepath = filedialog.askopenfilename(filetypes = fTyp,initialdir = iDir) file4.set(filepath) # button2クリック時の処理 def button2_clicked(): messagebox.showinfo('FileReference Tool', u'参照ファイルは↓↓\n' + file1.get()) messagebox.showinfo('FileReference Tool', u'参照ファイル2は↓↓\n' + file4.get()) # [指定したファイルを処理するメソッド] をここに messagebox.showinfo('処理ファイル選択', u'処理終了') root.quit() # rootを閉じる if __name__ == '__main__': # rootの作成 root = Tk() root.title('処理ファイル選択') root.resizable(False, False) # Frame1の作成 frame1 = ttk.Frame(root, padding=10) frame1.grid() # 参照ボタンの作成 button1 = ttk.Button(root, text=u'参照', command=button1_clicked) button1.grid(row=0, column=3) # ラベルの作成 # 「ファイル」ラベルの作成 s = StringVar() s.set('ファイル>>') label1 = ttk.Label(frame1, textvariable=s) label1.grid(row=0, column=0) # 参照ファイルパス表示ラベルの作成 file1 = StringVar() file1_entry = ttk.Entry(frame1, textvariable=file1, width=50) file1_entry.grid(row=0, column=2) # Frame3の作成 frame3 = ttk.Frame(root, padding=5) frame3.grid() # 参照ボタンの作成 button4 = ttk.Button(root, text=u'参照2', command=button4_clicked) button4.grid(row=1, column=3) # ラベルの作成 # 「ファイル」ラベルの作成 s2 = StringVar() s2.set('ファイル>>') label3 = ttk.Label(frame3, textvariable=s2) label3.grid(row=1, column=0) # 参照ファイルパス表示ラベルの作成 file4 = StringVar() file4_entry = ttk.Entry(frame3, textvariable=file4, width=50) file4_entry.grid(row=1, column=2) # Frame2の作成 frame2 = ttk.Frame(root, padding=(0,5)) frame2.grid(row=3) # Startボタンの作成 button2 = ttk.Button(frame2, text='Start', command=button2_clicked) button2.pack(side=LEFT) # Cancelボタンの作成 button3 = ttk.Button(frame2, text='Cancel', command=quit) button3.pack(side=LEFT) root.mainloop() |
コメント