How to Show File Path with Browse Button in Python / Tkinter

后端 未结 2 946
故里飘歌
故里飘歌 2021-02-11 05:44

Working with Python and Tkinter, I have been trying to find out the way to show the file_path beside the Browse Button but unable to do so.

Here is my code:

<         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-11 05:55

    Here is a diff that fixes instead the file_path, i.e. the StringVar() usage:

    --- old.py      2016-08-10 18:22:16.203016340 +0200
    +++ new.py      2016-08-10 18:24:59.115328029 +0200
    @@ -4,7 +4,6 @@
    
    
     content = ''
    -file_path = ''
    
    
     #~~~~ FUNCTIONS~~~~
    @@ -16,7 +15,7 @@
       filename = askopenfilename()
       infile = open(filename, 'r')
       content = infile.read()
    -  file_path = os.path.dirname(filename)
    +  file_path.set(os.path.dirname(filename))
       return content
    
     def process_file(content):
    @@ -40,7 +39,7 @@
     f2 = Frame(mf, width=600, height=250)
     f2.pack()
    
    -file_path = StringVar
    +file_path = StringVar(root)
    
    
     Label(f1,text="Select Your File (Only txt files)").grid(row=0, column=0, sticky='e')
    

提交回复
热议问题