我正在制作一个程序,使用askopenname文件对话框来选择文件,然后我想将目录保存到一个字符串中,以便我可以使用另一个预先设定的函数(我已经创建了)将文件提取到不同的位置。
打开文件对话框的按钮代码如下:
`a = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))`
我正在制作一个程序,使用askopenname文件对话框来选择文件,然后我想将目录保存到一个字符串中,以便我可以使用另一个预先设定的函数(我已经创建了)将文件提取到不同的位置。
打开文件对话框的按钮代码如下:
`a = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))`
import tkinter
import tkinter.filedialog
import getpass
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
# Get the file
file = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user)
# Split the filepath to get the directory
directory = os.path.split(file)[0]
print(directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()
tix.FileEntry(self, dialogtype='tixDirSelectDialog')
一样,但没有使用tix库? - pihentagyfrom tkFileDialog import askdirectory
directory= askdirectory()
然后在代码中:
import tkinter
import tkinter.filedialog
import getpass
from tkFileDialog import askdirectory
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
directory= askdirectory()
print (directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()