Hello i am trying to create a button to get a file directory using tkinter (i am fairly new). I get the button to pop up and the function i created works. The problem is when the function is returning the value all i get is ".!button" or ".!button1" instead of the file directory. Help would be appreciated
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:
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()
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 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()
