Python Forum
[TKINTER] Problems creating directories in a selected path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TKINTER] Problems creating directories in a selected path
#1
Hello.

I believe that my problem is easy to be solved, but I've already researched it in forums and other websites but I couldn't solve it.

I'm trying to create a small application, which opens a window with TKINTER and when I click on the button and select a directory. This application must create other directories in the path that was indicated when selecting the button.

My code
import os
from tkinter import *
import tkinter.filedialog

janela = Tk()
janela.title('TESTE')
janela.geometry('200x200')

def createDirectory():
    os.chdir(sourcePath)
    directory = 'Data'
    os.makedirs(directory)

def selectDirectory():
    sourcePath = filedialog.askdirectory()
    createDirectory()
    
selectButton = Button(janela, text='Select Diretory', width=15, command=selectDirectory)
selectButton.grid(column=0, row=0, padx=10, pady=10)

janela.mainloop()
This is the error message, that i'm receiving.

Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1883, in __call__ return self.func(*args) File "<ipython-input-2-737f6accf91d>", line 16, in selectDirectory createDirectory() File "<ipython-input-2-737f6accf91d>", line 10, in createDirectory os.chdir(sourcePath) NameError: name 'sourcePath' is not defined
Reply
#2
You need to pass the sourcePath

import os
from tkinter import *
import tkinter.filedialog
 
janela = Tk()
janela.title('TESTE')
janela.geometry('200x200')
 
def createDirectory(sourcePath):
    os.chdir(sourcePath)
    directory = 'Data'
    os.makedirs(directory)
 
def selectDirectory():
    sourcePath = filedialog.askdirectory()
    createDirectory(sourcePath)
     
selectButton = Button(janela, text='Select Diretory', width=15, command=selectDirectory)
selectButton.grid(column=0, row=0, padx=10, pady=10)
 
janela.mainloop()
Vulera likes this post
Reply
#3
Thank you for answering my question. Big Grin

(Aug-10-2021, 07:57 AM)Larz60+ Wrote: You need to pass the sourcePath

import os
from tkinter import *
import tkinter.filedialog
 
janela = Tk()
janela.title('TESTE')
janela.geometry('200x200')
 
def createDirectory(sourcePath):
    os.chdir(sourcePath)
    directory = 'Data'
    os.makedirs(directory)
 
def selectDirectory():
    sourcePath = filedialog.askdirectory()
    createDirectory(sourcePath)
     
selectButton = Button(janela, text='Select Diretory', width=15, command=selectDirectory)
selectButton.grid(column=0, row=0, padx=10, pady=10)
 
janela.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems trying to position images with Tkinter emont 3 670 Dec-12-2023, 07:20 AM
Last Post: menator01
  Creating a function interrupt button tkinter AnotherSam 2 5,416 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Python - Tkinter : How can I send path Excel from GUI to scripte johnjh 2 4,236 Aug-28-2020, 05:38 PM
Last Post: maaad
  Tkinter having problems with packing labels? wallgraffiti 0 1,507 Aug-02-2020, 09:26 AM
Last Post: wallgraffiti
  [Tkinter] Problems to display Web Scraping values in Tkinter Lucas_Ribeiro 0 1,535 May-07-2020, 12:36 AM
Last Post: Lucas_Ribeiro
  [Tkinter] Tkinter custom widget styling and creating custom theme karolp 6 4,745 May-06-2020, 06:11 PM
Last Post: karolp
  Creating and destroying dynamic labels in Tkinter MarcusRoberts 1 4,244 May-02-2020, 06:49 PM
Last Post: Yoriz
  tkinter.TclError: bad window path name kenwatts275 3 14,631 Apr-26-2020, 08:16 PM
Last Post: kenwatts275
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,535 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  tkinter frame sizing problems Lux 0 4,868 Aug-26-2017, 06:09 PM
Last Post: Lux

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020