2016-09-16 71 views
0

所以這裏是我的問題,我只是無法弄清楚,似乎無法找到信息來幫助我理解正在發生的事情。所以我設置了source等於調用openDirectory這個函數的按鈕,這個函數實際上只是一個快捷方式,在tkinter.filedialogaskopendirectory函數上調用os.path.join()os.path.normalize()如何將一個變量設置爲tkinter Button的命令函數的結果?

問題是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()), "") 

請注意,您openDirectory方法具有listfiles參照實例本身,而其他方法使用傳統的self - 我已經改變了它使用self所以你不必處理listfiles.destination = ...

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

請注意,我已經改變了參數messagebox.askokcancel字符串'Window Title'和參考self.destination,所以不是窗口的標題是對框架的引用(它應該是窗口的標題字符串)它是一個實際的字符串,而不是消息的文本是對按鈕的引用(它應該是消息文本的字符串),而是保存在openDirectory中的字符串。

+0

你搖滾!我從來沒有這樣想過,但基於你給出的解釋,它是非常有意義的。非常感謝! – balfred