所以这里是我的问题,我只是无法弄清楚,似乎无法找到信息来帮助我理解正在发生的事情。所以我设置了source
等于调用openDirectory
这个函数的按钮,这个函数实际上只是一个快捷方式,在tkinter.filedialog
的askopendirectory
函数上调用os.path.join()
和os.path.normalize()
。如何将一个变量设置为tkinter Button的命令函数的结果?
问题是source
始终是一个数字,我不明白为什么它不是我在openDirectory
函数中选择的路径。我也尝试将openDirectory
中的代码直接放在按钮的命令中,它仍然执行相同的操作。
重现步骤:
- 运行这段代码(使用Python 3.5编写)
- 使用源按钮
- 在右下角点击该按钮应显示在路径选择路径a
messagebox
- 请注意,
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()
可能需要帮助: http://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – wbrugato