Unable to select multiple files in tkinter - python

I'm unable to select multiple files using tkinter's askopenfilenames on linux. I can select multiple files on windows but not on linux.
path = list(askopenfilenames(filetypes=('Images','*.jpg *.jpeg *.png')))

The following code will allow you to select multiple files. You need to hold Control or Shift as you select each file
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
path = list(filedialog.askopenfilenames(filetypes=[('Images','*.jpg *.jpeg *.png')]))
print(path)

Related

Browsing file and getting filepath in entrybox in Tkinter

I am trying to make a simple GUI using Tkinter in python2, in which I need to make an entry box and a button besides that. The button browses the file and shows the filepath in the entrybox. How can I do that.
I used the tkFileDialog.askopenfilename that allows to browse the path but how can I make the gui to show that path in an entry box.
I tried it as follows:
import tkinter as tk
import tkFileDialog
root=tk.Tk()
def browsefunc():
filename =tkFileDialog.askopenfilename(filetypes=(("tiff files","*.tiff"),("All files","*.*")))
ent1=tk.Entry(frame,font=40)
ent1.grid(row=2,column=2)
b1=tk.Button(frame,text="DEM",font=40,command=browsefunc)
b1.grid(row=2,column=4)
root.mainloop()
Attached a screenshot of what I needs.
Are you really sure, that you are using python2? Because you wrote tkinter with a lowercase t and not with an uppercase T or did you just write it wrong?.
Anyway, you can easily insert a little text (in your case a path) into your Entry-widget by using the insert method of the Entry-widget. In your case it would be:
import Tkinter as tk
import tkFileDialog
root=tk.Tk()
ent1=tk.Entry(root,font=40)
ent1.grid(row=2,column=2)
def browsefunc():
filename =tkFileDialog.askopenfilename(filetypes=(("tiff files","*.tiff"),("All files","*.*")))
ent1.insert(tk.END, filename) # add this
b1=tk.Button(root,text="DEM",font=40,command=browsefunc)
b1.grid(row=2,column=4)
root.mainloop()
The tk.END parameter gives the last index of the entry-string back.
If you already wrote something into the Entry-Widget like that:
This is my path:
and you add your path, than it will looks like that:
This is my path:/usr/bin/...
As you can see it adds the string in the end of the "entry-string".
The other option would be 0 for the index than your path will be in the beginning of the entry-widget:
/usr/bin...HI
I'm sorry if my english is horrible! Feel free to edit it!
May be try this code it might work
from tkinter import *
from tkinter.filedialog import askopenfilename
root=Tk()
ent1=Entry(root,font=40)
ent1.grid(row=2,column=2)
def browsefunc():
filename = askopenfilename(filetypes=(("jpg file", "*.jpg"), ("png file
",'*.png'), ("All files", "*.*"),))
ent1.insert(END, filename) # add this
b1=Button(root,text="DEM",font=40,command=browsefunc)
b1.grid(row=2,column=4)
root.mainloop()

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.

Is it possible to use tkinter to create a a directory select dialogue which can also display the files in that directory?

I want to use tkFileDialog to create a directory select dialogue, but also can display the files in that directory which can only be seen, not selected.
from tkFileDialog import *
......
def select_dir(self):
d = askdirectory()
if d:
......
But it seems the askdirectory() can't display the files in the directory. Is it possible to use askdirectory() implement my requirement?
No, you cannot use the built-in dialogs in this way. They work as documented, and only as documented. You will need to build your own dialog from a Toplevel window and various other widgets.

Manipulating an OS directory with Tkinter

I'm writing a function that allows my Tkinter GUI app user browse a directory and choose a file from it. So far, I have the code to open the directory but I'm having problems with saving a selected file from that directory to a variable that I can play around with.
The code I have so far - :
import os
def browsetone(self):
os.startfile("C:\Users\Chidumaga\Music\music")
The music directory is opened but how do I register the selection of a file ? Thanks in advance.
It is not very clear for me what you are trying to do. Anyway, it is tagged with Tkinter so I guess this is what you need:
from tkinter import *
from tkinter import filedialog
guiRoot = Tk()
startDir = "C:\Users\Chidumaga\Music\music"
someFileName = filedialog.askopenfilename(parent=guiRoot,title='Choose a file',initialdir=startDir)
if(someFileName!=""):
someFile = open(someFileName,'rb')
#read file contents
someFile.close()
guiRoot.mainloop()
Obviously opening a file dialog should be triggered by a button click or some similar event. It is up to you. Example of how to read binary file: Reading binary file in Python and looping over each byte

Categories