2016-12-13 8 views
-1

特定のファイルを含むディレクトリを参照するためにtkinterで参照ボタンを作成します。私はファイルをコピーして特定のディレクトリ内のこれらのファイルに対していくつかのコントロールを実行するプログラムを作っていますが、ディレクトリは時々変わることがあります。ブラウズボタンは開始ページにあり、ディレクトリを選択してから別のボタンをクリックすると、プログラム内のコントロールの1つが実行され、コントロール中にディレクトリのファイルが使用されます。私はまた、どのディレクトリが選択されているかを述べるべき項目を追加しました。ディレクトリをブラウズして後で使用するためのパスを保存する方法を知っている人はいますか?エントリとブラウズで選択されたディレクトリを結びつける方法は?ディレクトリtkinterを参照

from tkinter import * 
import os 

LARGE_FONT = ("Verdana", 12) 
current_dir = os.getcwd() 


class Gui(Tk): 

def __init__(self, *args, **kwargs): 
    Tk.__init__(self, *args, **kwargs) 

    Tk.wm_title(self, "My controls") 

    container = Frame(self) 
    container.pack(side="top", fill="both", expand=True) 
    container.grid_rowconfigure(0, weight=1) 
    container.grid_columnconfigure(0, weight=1) 

    self.geometry("350x500") 

    self.frames = {} 

    frame = StartPage(container, self) 

    self.frames[StartPage] = frame 

    frame.grid(row=0, column=0, sticky="nsew") 

    self.show_frame(StartPage) 

def show_frame(self, cont): 

    frame = self.frames[cont] 
    frame.tkraise() 


class StartPage(Frame): 

def __init__(self, parent, controller): 
    Frame.__init__(self, parent) 
    label = Label(self, text="My controls",  font=LARGE_FONT) 
    label.pack(pady=10, padx=10) 
    self.buttons() 
    self.program = "" 

*#Defining the browsebutton, any suggestions on what I should do next?** 

def browsedir(self): 
    from tkinter.filedialog import askdirectory 

    Tk().withdraw() 
    self.filename = askdirectory() 

*#Defining the copying of the files and filepath. How to make the filepath variable?* 

def runanalyze1(filepath): 
    for txt in os.listdir(filepath): 
    if txt.endswith('.txt'): 
     copyfiles(from_path=filepath+"\\"+txt, to_path=r"C:\catalog"+"\\"+txt) 

def buttons(self): 

    self.pack(fill=BOTH, expand=1) 
    analyze1button = Button(self, text="Copyfiles", command=self.runanalyze1) 
    analyze1button.place(x=50, y=150) 

    quitbutton = Button(self, text="Quit", command=self.quit) 
    quitbutton.place(x=150, y=250) 

    *#Creating the browse button* 

    browsebutton = Button(self, text="Browse", command=self.browsedir) 
    browsebutton.pack(side=BOTTOM) 

    *#Creating an entry, not sure how to tie this to the browse button either.* 

    e1 = Entry(self, bd=5) 
    e1.pack(side=BOTTOM) 
    e1.insert(0, "{}".format(current_dir)) 

if __name__ == "__main__": 
    app = Gui() 
    app.mainloop() 

注:

マイTkinterのスクリプトは、この(作業中)のようになり、私は制御等の残りのためのより多くのコードを持って、私は質問に関連するものを追加しました。 Pythonでの作業3.4.1。答えに感謝します!

+0

コードはすでにユーザーがディレクトリを参照できるように見え、変数に保存しています。なぜあなたは助けを必要としますか?あなたの現在のソリューションについて、あなたが気に入らないものは何ですか? –

+0

これは機能しません。プログラムは、コードがこのようなときにディレクトリとファイルを見つけることができません。だから私は間違ったことやもっと何かできることがなければならないと思ったが、自分でそれを理解することはできなかった。 – Pexe

+0

「プログラムがディレクトリを見つけることができません」という意味はどうですか? 'askdirectory'を呼び出すと、プログラムは何もしません。ディレクトリを検索して見つけるユーザーです。いくつかのディレクトリがダイアログに表示されていないと言っていますか? –

答えて

0

os.pathモジュールを読んでみてください。パスの発見にはいくつか興味深いことがあります。これらを絶対パスなので、文字列変数として保存することができます。もう1つの便利なことは、os.getcwd()またはos.path.dirname(__file__)です。これらは、現在のディレクトリへのパスとほぼ同じものを提供します。最初にファイルが実行されたディレクトリを指定し、2番目にファイルがあるディレクトリを指定します。

+0

ありがとう、私はそれを調べます! – Pexe

関連する問題