1

I have a GUI where I ask the user to select the directory and create a folder. Then it has the user browse for a file. That file is then read and directed to an absolute file path. I would, instead, like that path to be the user selected path and folder and cannot figure that out! Please help. _/_

from tkinter import *
from tkinter import ttk, filedialog
from tkinter.filedialog import askopenfile
import os
import pandas as pd

def selectPath():   
    path_ = askdirectory()
    path.set(path_)
def create_subfolder():  
    print("folder_name: ", folder.get())
    print("path_name: ", path.get())
    dirs = os.path.join(path.get(), folder.get())
    if not os.path.exists(dirs):
        os.makedirs(dirs)
        tkinter.messagebox.showinfo('Tips:','Folder name created successfully!')
    else:
        tkinter.messagebox.showerror('Tips','The folder name exists, please change it')

path = tk.StringVar()   # Receiving user's file_path selection
folder = tk.StringVar() # Receiving user's folder_name selection        


tk.Entry(window,textvariable = path).place(relx=0.05, rely=0.12, relheight=0.06, relwidth=0.30)
tk.Button(window, text = "Select your directory: ", command = selectPath).place(relx=0.37, rely=0.11, relheight=0.08, relwidth=0.30)

tk.Entry(window,textvariable = folder).place(relx=0.05, rely=0.20, relheight=0.06, relwidth=0.30)
tk.Button(window, text = "Submit Folder Name: ", command = create_subfolder).place(relx=0.37, rely=0.19, relheight=0.08, relwidth=0.30)

def openFile():
    filename = filedialog.askopenfilename(initialdir = r'C:\ABC\123\MyFile',
                                          filetypes=[("Excel Files", "*.xlsx")])
    os.startfile(filename)
    df = pd.read_excel(filename, sheet_name = 'Order Details')
  
    df.to_excel(r'C:\ABC\123\MyFile_2.xlsx')

can this be df.to_excel(dirs)?

    
def onClick():
    tk.messagebox.showinfo("Importing My File",  "Your file imported!")
    
button = tk.Button(canvas, text='Import Intelliscan File', bg='LightYellow2', fg='black',command=openFile)
button.place(relx=0.05, rely=0.29, relheight=0.13, relwidth=0.30)  

window.mainloop()```

Thanks in advance!
Gina_G
  • 33
  • 6
  • What exactly can't you figure out? Getting the user's input? Making use of the user's input? -- which line of your example is mis-behaving? You might need to write a very minimal example that focuses specifically on what you are having trouble with - if tk isn't part of the problem don't include it in your answer - write something that gets a user's input for the path then tries to use it? – wwii Jul 09 '22 at 23:28
  • I would like this ```df.to_excel(r'C:\ABC\123\MyFile_2.xlsx') ``` to go to the user selected folder – Gina_G Jul 09 '22 at 23:51
  • Yes but which part is causing a problem? Are you able to obtain the user selected folder ? – wwii Jul 09 '22 at 23:57
  • Thank you for responding! There isn't a problem with this code other than I don't know how to change the absolute path from r'C:\ABC etc to the path directory that the user chose in ``` def selectPath(): path_ = askdirectory() path.set(path_) def create_subfolder(): print("folder_name: ", folder.get()) print("path_name: ", path.get())``` – Gina_G Jul 10 '22 at 00:05
  • Does [Build the full path filename in Python](https://stackoverflow.com/questions/7132861/build-the-full-path-filename-in-python) answer your question? – wwii Jul 10 '22 at 00:10
  • 1
    I looked at it, but I'm too new to know. I tried this ```def openFile(): filename = filedialog.askopenfilename(initialdir = r'L:\INBOX\USCA-DT-PCP\LCMS_PAMPA\IntelliScan_and_CIDS_Data\Intelliscan', filetypes=[("Excel Files", "*.xlsx")]) os.startfile(filename) df = pd.read_excel(filename, sheet_name = 'Order Details') df.to_excel(os.path.join(path + folder + "." + Intelliscan + xlsx))``` and got this error.... ```TypeError: unsupported operand type(s) for +: 'StringVar' and 'StringVar'`` – Gina_G Jul 10 '22 at 01:02
  • @user19255018, try `path.get() + folder.get()...` instead: https://stackoverflow.com/a/51121161/16136190. – The Amateur Coder Jul 11 '22 at 09:27
  • I was able to get it! Now I'm wanting to turn this destination into a module that can be used in other py files. that's getting messy too. – Gina_G Jul 11 '22 at 14:45

0 Answers0