2016-11-14 89 views
-1

我试图得到一个文件路径与askopenfilename()函数的文件路径,但我不能代替我的条目(myEntry)与选择问与Tkinter的蟒蛇

我如何处理这个文件路径的价值?

我的代码:

from tkinter import * 
from tkinter import filedialog 

class Window(Tk): 
    def __init__ (self,inTitle="FUNCT"): 
     Tk.__init__(self) 
     self.title(inTitle) 
     self.geometry("500x300") 
     self.__myEntry = StringVar(self,"E:/TEST.txt") 
     pathfile = Entry(self,textvariable = self.__myEntry, width =80) 
     pathfile.grid() 
     bouton1 = Button(self, text = "Parcourir", command =self.loadfile) 
     bouton1.grid() 

    def loadfile(inSelf): 
     global filename 
     inSelf.filename = filedialog.askopenfilename() 
     return inSelf.filename 

myWindow = Window() 
myWindow.mainloop() 

回答

0

代替返回filename的,您可以直接更改按钮的回调内部的StringVar值。

def loadfile(self): 
    self.__myEntry.set(filedialog.askopenfilename()) 

或由布赖恩·奥克利在意见建议,可以彻底删除StringVar和直接更新Entry

class Window(Tk): 
    def __init__ (self,inTitle="FUNCT"): 
     Tk.__init__(self) 
     self.title(inTitle) 
     self.geometry("500x300") 
     self.pathfile = Entry(self, width =80) 
     self.pathfile.grid() 
     self.pathfile.insert(0, "E:/TEST.txt") #inserts default filename 
     bouton1 = Button(self, text = "Parcourir", command =self.loadfile) 
     bouton1.grid() 

    def loadfile(self): 
     filename = filedialog.askopenfilename() 
     self.pathfile.delete(0,END) #removes current text 
     self.pathfile.insert(0,filename) #insert the filename 
+0

您还可以摆脱'StringVar'并直接更新条目窗口小部件。 –

+0

@BryanOakley当然。编辑该替代方案也是如此。感谢您的建议。 :) – Lafexlos

+0

这种方法是我的问题的一个很好的解决方案 – BORUSSEN11