I'm trying to create a button to get a file directory using tkinter. I get the button to pop up and the function I created works. The problem is when the function returns the value, I get ".!button" or ".!button1" instead of the file directory.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
return filedialog.askdirectory()
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
print(btnFind)
gui.mainloop()
CodePudding user response:
The line print(btnFind) does not print the return value of the function bound to the button press, it prints out the name of the button itself. In fact, returning something from the function isn't useful because you can't access the return value. It is better to create a global variable to store the selected folder, or, in this case, print it straight from the function:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
print(filedialog.askdirectory())
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
gui.mainloop()
To store the value in a global variable use the global keyword:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("100x100")
folder = None # Declare global variable first
def getFolderPath():
global folder
folder = filedialog.askdirectory()
btnFind = ttk.Button(gui, text="Open Folder",command=getFolderPath)
btnFind.grid(row=0,column=2)
gui.mainloop()
Here is an example of how you might use a global variable to store the folder, and then use it later:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
gui = Tk()
gui.geometry("200x100")
folder = None
def getFolderPath():
global folder
folder = filedialog.askdirectory()
def onFolderConfirmed():
global folder
if folder is None:
strvarError.set("Please select a folder first")
else:
doSomethingWithFolder()
def doSomethingWithFolder():
... # Your code here
btnFind = ttk.Button(gui, text="Open Folder", command=getFolderPath)
btnFind.grid()
btnConfirm = ttk.Button(gui, text="Confirm Folder Choice", command=onFolderConfirmed)
btnConfirm.grid(row=1)
strvarError = StringVar()
lblError = Label(gui, textvar=strvarError)
lblError.grid(row=2)
gui.mainloop()
Here I added another button to confirm the choice of folder. If the user presses this without having selected a folder, it prompts them to select one; if not it executes some other code. If you wanted the other code to be run as soon as a folder is selected, just put that code straight in the getFolderPath function.
CodePudding user response:
You are printing button var... Simply do:
from tkinter import *
from tkinter import filedialog, ttk
gui = Tk()
gui.geometry("100x100")
def getFolderPath():
file = filedialog.askdirectory()
#do what you want with file
ttk.Button(gui, text="Open Folder",command=getFolderPath).grid(row=0,column=2)
gui.mainloop()
