Search code examples
pythonuser-interfacefilesystemsnativecustomtkinter

How to create an File Dialog box in CustomTkinter?


Please review this code where I have used CustomTkinter (alias as "ctk") to create a button. The "command" field contains a function "selectfile". I want to implement this "selectfile" function.

button_to_select = ctk.CTkButton(app, text = "Choose file", fg_color = "blue", command = selectfile)
button_to_select.pack(padx = 25, pady = 25)
button_to_select.place(x = 825, y = 300)

So we have always seen applications opening up another window for other works such as Logins or File Selections. I want to create the same here. The button on click will open up a window of the Windows Explorer and let users select any number and any type of files. We will also have an "Ok" button that will close the window and send the files back to the application for further work.

How to implement this function? So it will be like when we want to send some files into WhatsApp, the Windows Explorer opens and we select the files... This would be something similar. And no is not an Web-app. It is a Native App.

I am unable to create anything because I lack knowledge. All I tried to do is use the "subprocess" module to run the Windows Explorer. But that just runs the explorer separately and does not do the expected work as I described.


Solution

  • You can use filedialog.

    Use this function to open the windows file selector

    from customtkinter import filedialog    
    def selectfile():
            filename = filedialog.askopenfilename()
            print(filename)
    

    Edit : Customtkinter got this function