Get a file's directory in a string selected by askopenfilename

前端 未结 2 702
小蘑菇
小蘑菇 2021-01-21 18:20

I\'m making a program that you use the askopenname file dialog to select a file, which I then want to save the directory to a string so I can use another function (which I alrea

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 18:37

    If you know where the file actually is, you could always just ask for a directory instead of the file using:

    from tkFileDialog  import askdirectory  
    directory= askdirectory()
    

    Then in the code:

    import tkinter
    import tkinter.filedialog
    import getpass
    from tkFileDialog  import askdirectory
    # Need this for the `os.path.split` function
    import os
    gui = tkinter.Tk()
    user = getpass.getuser()
    def click():
        directory= askdirectory()
        print (directory)
    button = tkinter.Button(gui, command=click)
    button.grid()
    gui.mainloop()
    

提交回复
热议问题