Selecting directory's path with tkinter - python

I'm trying to get the full path of a directory using Tkinter, but only the directory name is being displayed.
For example when I select "C:\Python27\Doc", instead of the full path being displayed, only "Doc" displays.
class Actions:
def openfile(self): #open the file
directory = tkFileDialog.askdirectory()
print(directory)
def body(self):
Label (text='Please select a directory').pack(side=TOP,padx=10,pady=10)
I found this http://tkinter.unpythonic.net/wiki/tkFileDialog , but unless I'm misunderstanding it, I don't see anything for the full path.

Have you tried the initialdir keyword? e.g. tkFileDialog.askdirectory(initialdir='.'). What do you get then?

Related

tkinter askdirectory syntax

I have a question about the askdirectory() in tkinter. Is it posible to use that function and also se what´s inside the folder that i want to select the directory from?
Because now when i use the function i can open the explorer and get the directory path to the folder i need but i can´t se what the folder contains (I just now that now before hand for now)... With the askdirectory function the folder says "No items match your search.". So i came up with this:
filepath_ask = filedialog.askdirectory(
initialdir=os.path.dirname(filedialog.askopenfilename(title ="Pick a folder in directory with .log files")),
title = "Press 'Select Folder'")
But it´s not that "user friendly". First it opens a window with askopenfilename so that i can see the content in the folder, then it closes when i select a file and opens a new window with askdirectory to "Select Folder" that has the content/file i chose in the window before. There must be a better way? I have been reding upp on the dokumentatin but can´t find anything that works. Help would be appreciated! Thanks
If you think that it isn't as much of a bother to have the user select a file in the askopenfilename dialog, then why not just run with that (and skip askdirectory altogether):
import tkinter as tk
from tkinter import filedialog
import pathlib
root = tk.Tk()
ask = filedialog.askopenfilename(title="Select a directory", filetypes = [("log",".log"),("All Files",".*")])
print(f"User selected Directory: {pathlib.Path(ask).resolve().parent}")
root.destroy()

Python tkinter : browse directory and save to new directory

I would like to open a new browser by clicking Button from python tkinker GUI and new directory need to be saved and display on GUI.
I am able to open current directory with command below;
import os
subprocess.Popen('explorer "C:\temp"')
cur_path = os.path.dirname(__file__)
my question is how to save the active browser dir and display on GUI after Step A/B above?
First of all, the imports needed for this answer:
import os
import tkinter as tk # if using Python 3
import Tkinter as tk # if using Python 2
Let's say that your button has been defined.
Here is some sample code which will get the current directory:
curr_directory = os.getcwd() # will get current working directory
If you're looking to set up a GUI to ask the user to select a file, use:
name = tkinter.tkFileDialog.askopenfilename(initialdir = curr_directory,title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print(name)
Which will store the file that they have chosen, with the directory they start at set to curr_directory which is the current directory.
If you are instead looking to set up a GUI in which the user chooses a directory, you can use:
dir_name = tk.tkFileDialog.askdirectory()
This will store the name of the directory they have chosen in the dir_name variable.
For more information, check out this link on how to use the file dialog. Alternatively, you can check the general tkinter documentation here (for Python 2) and here (for Python 3). If you need a reference to the file dialog, this is a good source.

PyQt - assign to a button the ability to choose a directory

I am having trouble searching for the method to assign to a Push Button the ability to choose a directory. I found this how to have a directory dialog in Pyqt, but I am still unsure about the method.
For example, I have a push button called new_directory and I have this code self.new_directory.clicked.connect(self.pick_new). What do I need to put into the function pick_new so that when new directory is clicked I can choose a new directory and have this stored in a variable?
Thanks!
I think this might help you.
With this you can get the directory.
def pick_new():
dialog = QtGui.QFileDialog()
folder_path = dialog.getExistingDirectory(None, "Select Folder")
return folder_path

In python is there a way to use the windows explorer to create a file path which can then be returned as a string?

I am making a program to manage a database and i need a nice way of choosing the save locations of files within the program. I was wondering if it is possible to open windows explore from my program, select a folder to save a file in, enter the file name and return the file path as a string to the main program.
Look up the Tkinter options in tkFileDialog
I don't think you necessarily need to put together a fully working GUI, you can simply call the method and select a folder location and then use that selected location in your code.
A simplistic example would be:
import Tkinter, tkFileDialog
root = Tkinter.Tk()
x = tkFileDialog.askopenfilename() # Can pass optional arguments for this...
root.destroy()

Use selected explorer file or folder path as a variable in a python script for a contextual menu

I'm writing a python script that takes a file one at a time or recursively through folders and moves them to a new location. The script takes one parameter (the current path of the file). I want to be able to use the selected item in an explorer window as the variable.
I am making a contextual menu through the regedit files that is labeled "Send to Server". I currently have the appropriate regedit files created and pointed to the location of the command python.exe "path\to\python\file.py
long story short, I want a contextual menu to pop up that says "Send to Server" when a file is right clicked and when executed uses the selected file's or folder's path as the only variable I need. So far I have come across tkFileDialog (not quite what I want) ctypes and win32 modules but I can't quite figure out the last three modules or whether or not they will help
As a side note. I have created a python script that does this exact thing on mac osx. Much easier with macs 'services' feature.
If you put a shortcut to this script (written for Python 3) in the user's "SendTo" folder (%USERPROFILE%\SendTo), it will pop up a directory dialog when selected from the right-click SendTo menu. The dialog works for network locations as well. When the script runs, the full path to the selected file/folder is in sys.argv[1]. Currently it just shows the selected destination path in a message box. You can change the extension to pyw if you don't want a console.
import os, sys
from tkinter import Tk, filedialog
from tkinter.messagebox import showinfo
class Dialog:
def __init__(self, path):
self.path = path
self.dst_path = ''
self.root = root = Tk()
root.iconify()
root.after_idle(self.askdirectory)
root.mainloop()
def askdirectory(self):
self.dst_path = filedialog.askdirectory(initialdir=self.path)
showinfo('Selected Path', self.dst_path)
self.root.destroy()
if __name__ == '__main__':
if len(sys.argv) > 1:
path = sys.argv[1]
if os.path.isfile(path):
path = os.path.dirname(path)
dialog = Dialog(path)
#if dialog.dst_path: do_something(dialog.dst_path)

Categories