Python: easygui, how to select multiple files? - python

I am using fileopenbox() and I want to select all text files I have when the windows box is open. I have tried to press shift or ctrl + A, but it didn't work.
openfile = fileopenbox("Welcome", "COPR", filetypes= "*.txt")

You can select multiple files if you include multiple=True in the arguments:
openfiles = fileopenbox("Welcome", "COPR", filetypes= "*.txt", multiple=True)
Note that now fileopenbox will return not a string, but a list of strings like:
["foo.txt", "Hello.txt", "mytxt.txt"]

another option could be using tkinter as follows(python 3.x):
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
files = filedialog.askopenfilenames(parent=root, initialdir="/", title='Please select files')

It's not possible with easygui. What you can do is reuse the code from easygui (see line 1700) and modify it slightly to use askopenfilenames instead of askopenfilename.

Related

How to make app from tkinter that can be open in other computers?

I want to make an application from tkinter and I converted it from py into exe files using pyinstaller, and I want the application can be use for public. But the problem is if I use it in other computer it doesn't work, because there are files that support the application. Is it possible if I made that application and use it to public? And it it's possible, what's the specific code that I can use in pyinstaller? Thanks
If your application needs attached files to work, you can store all of them in a .zip file for example, along with your .exe, on an online server.
You can use GitHub Pages for this, because you can access the raw files' contents unlike secured file storages that only allow you to download them using a browser.
Then, you can create an installer for all the files that you stored in your .zip file. That installer is what you will give to the public. Below is an example that is demonstrated through the use of this GitHub repo: https://github.com/d-002/install-wizard/
The rudimentary installer that I created will download the .zip from the server containing everything needed, then it will unzip it into a directory that the user set before, then remove the temporary .zip. Try it out:
from urllib.request import *
from zipfile import ZipFile
from os import chdir, mkdir, remove
from os.path import join, exists
from tkinter import *
from tkinter.filedialog import askdirectory
from tkinter.messagebox import showinfo, showerror, showwarning
def browse():
global path
path['text'] = askdirectory()
def install():
if len(path['text']): # check if valid directory
try:
realpath = join(path['text'], 'application name')
if not exists(realpath):
mkdir(realpath) # create a subfolder with the application name
chdir(realpath) # move to that directory
# download the .zip containing all needed files
data = urlopen('https://d-002.github.io/install-wizard/application.zip').read()
with open('application.zip', 'wb') as f:
f.write(data)
# unzip the file
zip = ZipFile('application.zip')
zip.extractall()
zip.close()
# remove the temporary zip file
remove('application.zip')
showinfo('Done', 'Completed successfully')
except Exception as e:
showerror('Error', 'An error occured: %s' %e)
else:
showwarning('Error', 'Please enter a valid path')
def make_gui():
global tk, path
tk = Tk()
Label(tk, text='Path:').grid(row=0, column=0)
path = Label(tk, text='')
path.grid(row=0, column=1)
Button(tk, text='Browse', command=browse).grid(row=0, column=2)
Button(tk, text='Install', command=install).grid(row=1, column=1, columnspan=3, sticky='ew')
make_gui()
tk.mainloop()

Creating a subfolder in a folder opened with filedialog

I am currently trying to make a program for file organisation. I want my script to open a filedialog that asks for a sourcefolder where the files will be organised in based on their extension. After a sourcefolder has been selected, I want my script to make directories in that sourcefolder. I am a bit stuck on that last part.
import os
import shutil
from os import listdir
from os.path import isfile, join
import tkinter as tk
from tkinter import filedialog
from tkinter import *
print('This is a program that organises files in a given directory')
print(''
'')
print('Please select the folder in which you want to organise your files')
''' Source Folder '''
root = Tk()
root.withdraw()
source_path = filedialog.askdirectory()
The part above works just fine.
''' Create destination folders in the source folder '''
newpath1 = r'source_path\Images'
if not os.path.exists(newpath1): # check to see if they already exist
os.makedirs(newpath1)
newpath2 = r'source_path\Documents'
if not os.path.exists(newpath2):
os.makedirs(newpath2)
newpath3 = r'source_path\Else'
if not os.path.exists(newpath3):
os.makedirs(newpath3)
This, however, does not. It just removes files, or places them somewhere I cannot find them.
Any help would be greatly appreciated.
TLDR: how to make directories in a sourcefolder submitted with filedialog.

How to replace input string in python

I use tkinter make interface to select folder directory and i get C:/Users/dtung/Desktop/ and i want to convert it to C:\Users\dtung\Desktop\ because i use autoit to select file. this is my code
import_file_path = filedialog.askdirectory()
list = os.listdir(path=import_file_path)
import_file_path.replace("/","\\")
replace function does't work with any string or character(dont have any error it just does't work), when i print it out i just received old string.
I don't understand your problem. The code above works fine for me.
import os
from tkinter import filedialog
import_file_path = filedialog.askdirectory()
list = os.listdir(path=import_file_path)
print('Before', import_file_path)
import_file_path = import_file_path.replace("/", os.sep)
print(os.path.join(import_file_path))
print('After', import_file_path)
prints out this on my Windows machine
Before C:/Users/Mega/Documents/HuaweiMusic
C:\Users\Mega\Documents\HuaweiMusic
After C:\Users\Mega\Documents\HuaweiMusic
By the way, why do you need to mix Autoit and Python?
You can use this code to fix the problem:
import os
from tkinter import filedialog
import_file_path = filedialog.askdirectory()
list = os.listdir(path=import_file_path)
import_file_path = import_file_path.replace("/", "\\")

Drag-and-Drop multiple files in Python (Windows)

I'm starting to learn Python, and I thought to create a converter from a file to another (for example, from png to avi or between other file extensions) under Windows OS for now.
I wrote a script which works fine and it completes the conversion process, but I want improve it in functionality (and then in graphics); I'm using Tkinter and I thought to load the files with the possibility to drag-and-drop them as input for the next conversion command, instead of open a folder in which to put files as "input source". I found this topic (python drag and drop explorer files to tkinter entry widget) and I used it in this way:
import sys
import os
import Tkinter
from tkdnd_wrapper import TkDND
import shlex, subprocess
from subprocess import Popen, PIPE
import glob
import shutil
root = Tkinter.Tk()
dnd = TkDND(root)
entry = Tkinter.Entry()
entry.grid()
def handle(event):
inputfilespath = event.data
event.widget.insert(0, inputfilespath)
filesdir = os.path.dirname(os.path.realpath(inputfilespath))
files = glob.iglob(os.path.join(filesdir, "*.myext"))
for inputfilespath in files:
if os.path.isfile(inputfilespath):
subprocess1 = subprocess.Popen([...conversion command given as list, not string...], shell=True)
print "\n\nConversione in corso..."
subprocess1.wait()
subprocess1.terminate()
print "\n\nProcesso terminato!"
dnd.bindtarget(entry, handle, 'text/uri-list')
root.mainloop()
The problems:
If filename has a space, there is no conversion, and process ends without even notify any error too. "inputfilespath" wants to be the generic name for all the input files which I selected, and (for what I read) I can't (?) use quotes for an environment variable hoping to include filename's whitespace...
I tried to copy different files (with the same file extension and without whitespaces into the filename) in the same folder, and if I drag-and-drop only one of them on the Entry widget, the process starts fine (and this is great!), but it continues also for all the other no-selected files with the same extension in the same folder, whereas if I drag-and-drop multiple files on the Entry widget, no conversion occurs....
It seems that filenames containing whitespace are being wrapped in braces
(Tcl list style). To get a usable filelist, you should be able to do
something like:
import Tkinter
from untested_tkdnd_wrapper import TkDND
def handle(event):
files = root.tk.splitlist(event.data)
for filename in files:
event.widget.insert('end', filename)
root = Tkinter.Tk()
lb = Tkinter.Listbox(root, width=50)
lb.pack(fill='both', expand=1)
dnd = TkDND(root)
dnd.bindtarget(lb, handle, 'text/uri-list')
root.mainloop()
Just use the tkinter file dialog, then just have it insert the files into the entry box.
Example:
filedialog = tkFileDialog.askopenfilenames(*options*)
entry.insert(END, filedialog)
Example Using StringVar:
entryVar = StringVar()
entry = Entry(textvariable=entryVar)
filedialog = tkFileDialog.askopenfilenames(*options*)
entryVar.set(filedialog
Hope this helps!

Python; User Prompts; choose multiple files

I would like to have my code bring up a window where you can select multiple files within a folder and it assigns these filenames to elements of a list.
Currently, I can only select a single file at a time and it assigns the filename to a single variable.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
Thank you.
You need to use the askopenfilenames method instead.
You can encapsulate all that in a function:
def get_filename_from_user(message):
root = Tk()
root.withdraw()
filename = tkFileDialog.askopenfilename(title=message)
return filename
Then you can call it as many times as you like:
filename1 = get_filename_from_user('select the first file!')
filename2 = get_filename_from_user('select another one!')
filename3 = get_filename_from_user('select one more!')
Unless you have tons of files you want to select. Then you probably want to use askopenfilenames:
files = tkFileDialog.askopenfilenames(parent=root,title='Choose a file or LOTS!')
from easygui import fileopenbox
files = []
#how many file you want choice
fileCount = int(input("How many file need open"))
for x in range(fileCount):
files.append(fileopenbox())
print(files)

Categories

Resources