PythonのTKinterで以下のようなイメージのGUIアプリを作ろうと思っています。
パス入力or参照ボタンでディレクトリ指定すると、そのディレクトリ内の全てのファイル、フォルダが下のボックス内に表示されるようにしたいです。
また、その中にテキストファイル(.txt)があった場合はファイル名の頭にチェックボックスをつけたいです。
現時点では真ん中の表以外のところはできています。「実行」の処理内容は後程実装しようと思い、今は適当なメッセージを出すようにしています。コードは以下です。
python
1import os,sys 2from tkinter import * 3from tkinter import ttk 4from tkinter import messagebox 5from tkinter import filedialog 6 7# フォルダ指定の関数 8def dirdialog_clicked(): 9 iDir = os.path.abspath(os.path.dirname(__file__)) 10 iDirPath = filedialog.askdirectory(initialdir = iDir) 11 entry1.set(iDirPath) 12 13 14# 実行ボタン押下時の実行関数 15def conductMain(): 16 text = "" 17 18 dirPath = entry1.get() 19 20 if dirPath: 21 text += "フォルダパス:" + dirPath + "\n" 22 23 if text: 24 messagebox.showinfo("info", text) 25 else: 26 messagebox.showerror("error", "パスの指定がありません。") 27 28if __name__ == "__main__": 29 30 # rootの作成 31 root = Tk() 32 root.geometry('400x300') 33 root.title("サンプル") 34 35 # Frame1の作成 36 frame1 = ttk.Frame(root, padding=10) 37 frame1.grid(row=0, column=1, sticky=E) 38 39 # 「フォルダ参照」ラベルの作成 40 IDirLabel = ttk.Label(frame1, text="フォルダ参照>>", padding=(5, 2)) 41 IDirLabel.pack(side=LEFT) 42 43 # 「フォルダ参照」エントリーの作成 44 entry1 = StringVar() 45 IDirEntry = ttk.Entry(frame1, textvariable=entry1, width=30) 46 IDirEntry.pack(side=LEFT) 47 48 # 「フォルダ参照」ボタンの作成 49 IDirButton = ttk.Button(frame1, text="参照", command=dirdialog_clicked) 50 IDirButton.pack(side=LEFT) 51 52 # Frame2の作成 53 frame2 = ttk.Frame(root, padding=10) 54 frame2.grid(row=5,column=1,sticky=W) 55 56 # 実行ボタンの設置 57 button1 = ttk.Button(frame3, text="実行", command=conductMain) 58 button1.pack(fill = "x", padx=30, side = "left") 59 60 root.mainloop()
詳しい方教えていただけると幸いです。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー