Jul-12-2018, 07:23 AM
(Jul-11-2018, 07:55 AM)Larz60+ Wrote: It doesn't have to be in __init__.
This is a habit of mine as I tend to access from several methods, but it can be used in local scope.
No, it doesn't work in local scope.
I moved it to local scope as the code below and you can try.
You'll find the label doesn't refresh correctly if changing the directory path from a long one to short.
import tkinter import tkinter.filedialog import os class fileData: def __init__(self): self.targetDir = "./" self.mainWin = tkinter.Tk() def reportShow(self): ln1 = 2 absPathv = tkinter.StringVar() tkinter.Label(self.mainWin, text = " Target ",justify = tkinter.LEFT).grid(row = ln1, column =0) absPath = os.path.abspath(self.targetDir) if len(absPath) > 40: absPath = ' ...%s' % absPath[-37:] absPathv.set(absPath) tkinter.Label(self.mainWin, textvariable = absPathv, background='white').grid(row = ln1, column = 1, columnspan = 3) def chooseFolder(): #This is where we lauch the directory change bar filesData.targetDir = tkinter.filedialog.askdirectory(title = "Choose the target directory", initialdir=filesData.targetDir) filesData.reportShow() def showMain(): mainWin = filesData.mainWin winSize = 360 mainWin.geometry('%dx%d+%d+%d' % (winSize, winSize, (mainWin.winfo_screenwidth()-winSize)/2, (mainWin.winfo_screenheight()-winSize)/2)) #The size and position of the main window mainWin.minsize(winSize,winSize) mainWin.bind('<Control-o>',lambda event: chooseFolder()) filesData.reportShow() mainWin.mainloop() filesData = fileData() if __name__=="__main__": showMain()