2016-09-16 9 views
0

ここに私の問題があります。私は理解できず、何が起こっているのか理解するのに役立つ情報を見つけることができないようです。だから私は本当にtkinter.filedialogからaskopendirectory機能にos.path.join()os.path.normalize()を呼び出すだけ短くカットされopenDirectoryという関数を呼び出すボタンに等しいsourceを設定します。tkinterボタンのコマンド機能の結果に変数を設定するには?

問題は、sourceは常に数値で、openDirectory機能で選択したパスではないことがわかりません。私もボタンのコマンドでopenDirectoryの中にコードを置こうとしましたが、それは同じことをやっています。再現する

ステップ:

  1. を実行し、このコード(のpython 3.5を使用して書かれた)
  2. ソースボタン
  3. を使用してパスを選択して、パスを表示する必要があります右下のボタンを押しますa messagebox
  4. messageboxには、パスの代わりに大きな番号が表示されます。

ソース変数に格納されたパスを取得して、いつでもアクセスできるようにするにはどうすればよいですか?

#!/usr/bin/python 
import os 
from functions import * 
from tkinter import * 
from tkinter import messagebox 
from tkinter import filedialog 


class FileMover(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self, parent) 
     self.parent = parent 
     self.initUI() 

    def openDirectory(listfiles, recursive): 
     destination = os.path.join(os.path.normpath(filedialog.askdirectory()), "") 
     return destination 

    def initUI(self): 
     self.parent.title("File Mover") 
     self.pack() 

     recursiveCheck = bool 
     previewCheck = bool 

     # source button and label. source should equal the path selected in openDirecotry 
     source = Button(self, text="Source Directory", command=lambda:openDirectory(recursiveCheck)) 
     sourceLabel = Label(self, text="Select a Source Directory...") 
     sourcemsg = Button(self, text="Source Variable", command=lambda:messagebox.askokcancel(self, source)) 

     # check box used to tell open directory either true or false to recurse the source dir 
     recursiveLabel = Label(self, text="Recursive ") 
     recursive = Checkbutton(self, onvalue=True, offvalue=False, variable=recursiveCheck) 

     # destination button and label. source should equal the path selected in openDirecotry 
     destination = Button(self, text="Target Directory ", command=lambda:openDirectory(False)) 
     destinationLabel = Label(self, text="Select a Target Directory...") 

     # not implemented yet 
     previewLabel = Label(self, text="Preview ") 
     preview = Checkbutton(self, onvalue=True, offvalue=False, variable=previewCheck) 

     source.grid(row=0, column=0, columnspan=2) 
     sourceLabel.grid(row=0, column=2) 
     recursiveLabel.grid(row=1, column=1) 
     recursive.grid(row=1, column=2, sticky=W) 
     destination.grid(row=2, column=0, columnspan=2) 
     destinationLabel.grid(row=2, column=2) 
     previewLabel.grid(row=4, column=6) 
     preview.grid(row=4, column=7, sticky=W) 
     # just for debugging to show source directory on demand 
     sourcemsg.grid(row=5, column=8) 

def main(): 
    root = Tk() 
    ex = FileMover(root) 
    root.mainloop() 

if __name__ == '__main__': 
    main() 
+0

助けてくれるかもしれません: http://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – wbrugato

答えて

1

ボタンは機能しません。関数がボタンのコールバックとして機能し、そのボタンを使用して関数を呼び出すと、どこにも戻りません。実際にその作業を行うための賢明な方法はありません。あなたが推測しているようにうまくいけば、ボタンへの参照を失うでしょう!あなたはそれを望んでいません。代わりに

、単にインスタンス変数として保存します。

def openDirectory(self, recursive): 
    self.destination = os.path.join(os.path.normpath(filedialog.askdirectory()), "") 

注意を他の方法は、従来のselfを使用しながら、あなたのopenDirectory方法は、インスタンス自体を参照listfilesを持っていたこと - 私が使用するように変更しましたselfだから、listfiles.destination = ...を扱う必要はありません。私は代わりに、ウィンドウのタイトルは、フレームへの参照であることの、文字列'Window Title'参照self.destinationmessagebox.askokcancelに引数を変更した

sourcemsg = Button(self, text="Source Variable", command=lambda:messagebox.askokcancel('Window Title', self.destination)) 

注意(ウィンドウのタイトルの文字列をすることになっています)それは実際の文字列だし、代わりに、メッセージのテキストがボタンへの参照であることの(メッセージのテキストの文字列ことになっている)、それはあなたがopenDirectoryに保存された文字列です。

+0

ロック!私はそのように考えたことはありませんでしたが、それはあなたが与えた説明に基づいて完璧な意味があります。どうもありがとうございます! – balfred

関連する問題