1

i wrote some code to make GUI program

import os
from tkinter import *
from tkinter import filedialog

dirName = os.getcwd()

def getDirName():
    global dirName
    dirName = filedialog.askdirectory()
    if dirName != ####:
        lbl_1_2.configure(text=dirName)
    else:
        pass

# lbl_1_2 = Label(app, text=dirName)
# btn_1 = Button(app, text="change", width=15, command=getDirName)

If I close file explorer without select directory (press esc or click [x])

then a value will return to dirName.

what is it? null? 0? trash? how can I get that value and insert to #### ?

thank you for reading

6
  • 1
    What is filedialog ? May 9, 2019 at 4:20
  • oops, tkinter.filedialog.askdirectory()
    – Naycccc
    May 9, 2019 at 4:21
  • Please edit your code to include all imports, how are you calling getDirName to reduce our guesswork May 9, 2019 at 4:22
  • btw there is no null in python ;) Did you try just if dirName:?
    – sashaaero
    May 9, 2019 at 4:23
  • @sashaaero wow. thank you so much. it works properly
    – Naycccc
    May 9, 2019 at 4:27

1 Answer 1

5

You set the directory to whatever you want with initialdir:

dirName = filedialog.askdirectory(parent=root,
                                  initialdir="/path/to/start",
                                  title='Please select a directory')

If the user clicks cancel instead of OK then the value returned is empty.

If you want to check if a value is set simply do:

if dirName:
    ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.