I tried to modify the Scrollbar from Tkdoc ( http://www.tkdocs.com/tutorial/morewidgets.html#scrollbar ) in order to show the filelist of a folder but it does not work. Can someone explain me why? and how to fix it.
Thanks a lot.
from tkinter import *
from tkinter import ttk
import os
def fileName():
path="C:\\temp"
dir=os.listdir(path)
for fn in dir:
print(fn)
root = Tk()
l = Listbox(root, height=5)
l.grid(column=0, row=0, sticky=(N,W,E,S))
s = ttk.Scrollbar(root, orient=VERTICAL, command=l.yview)
s.grid(column=1, row=0, sticky=(N,S))
l['yscrollcommand'] = s.set
ttk.Sizegrip().grid(column=1, row=1, sticky=(S,E))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
l.insert(fileName)
root.mainloop()
You should specify where to insert
l.insert(END, filename)
In your code, you insert function filename. Insert return value of filename function.
I renamed fileName
to get_filename
, and modified it to return filename list.
from tkinter import *
from tkinter import ttk
import os
def get_filenames():
path = r"C:\temp"
return os.listdir(path)
root = Tk()
l = Listbox(root, height=5)
l.grid(column=0, row=0, sticky=(N,W,E,S))
s = ttk.Scrollbar(root, orient=VERTICAL, command=l.yview)
s.grid(column=1, row=0, sticky=(N,S))
l['yscrollcommand'] = s.set
ttk.Sizegrip().grid(column=1, row=1, sticky=(S,E))
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
for filename in get_filenames():
l.insert(END, filename)
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.