Reputation: 353
I am trying to show/update the filename in the ttk.Label (variable Fname) without success, how can I do it? Any ideas?
Thanks.
My script:
import os
from tkinter import *
from tkinter import ttk
def printName():
path="X:\\Temp"
dir=os.listdir(path)
for fn in dir:
fName=path+'/'+fn
print(fName)
rt = Tk()
Frame = ttk.Frame(rt, padding="10 10 100 100")
Frame.grid(column=0, row=0, sticky=(N, W, E, S))
ttk.Button(Frame, text='Click', command=printName).grid(column=1, row=1, sticky=W)
ttk.Label(Frame, text="fName").grid(column=1, row=3, sticky=(W, E))
rt.mainloop()
Upvotes: 1
Views: 3512
Reputation: 20679
Another solution without a StringVar is just storing a reference to the Label widget and use its config
method to change the text option.
Apart from this, I recommend you to use os.path.join
instead of fName=path+'/'+fn
, and use another name for the variable dir
, since it is already a built-in function:
def printName():
path="X:\\Temp"
newtext = '\n'.join(os.path.join(path, fn) for fn in listdir)
label.config(text=newtext)
label = ttk.Label(Frame, text="fName")
label.grid(column=1, row=3, sticky=(W, E))
Upvotes: 1
Reputation: 728
What you want to do is use tkinter variable classes.
In your case, you want a StringVar
.
You can associate a Tkinter variable with a label (or basically any other widget). When the contents of the variable changes, the label is automatically updated:
v = StringVar()
Label(master, textvariable=v).pack()
v.set("New Text!")
Variable classes include BooleanVar, DoubleVar, IntVar, StringVar
Upvotes: 2