Home > Blockchain >  How show vertical list on Tkinter
How show vertical list on Tkinter

Time:01-15

I am starting in tkinter and I have generated a list with the elements that are in a certain folder and I need to show them in the interface. I put it in a label but it shows me the elements horizontally and I need it to show one below the other, is there a way to do this?

from tkinter import *
from os import listdir

raiz = Tk()
ruta = './imagenes'
fotos = Frame()
fotos.place(x=0,y=0)
fotos.config(bd=10,relief="groove",width="500", height="200")

fotografias = StringVar()
lblfotos = Entry(fotos, textvariable=fotografias)
lblfotos.config(width="75")
lblfotos.place(x=10,y=0)
fotografias.set(listdir(ruta))

raiz.mainloop()

https://i.stack.imgur.com/JaEek.png

[1]: P.S. The original idea is that the files in the folder are displayed in the interface and you can interact with them, such as opening or deleting, but I didn't find how, could that be done in tkinter? or maybe in another library?. Thank you for your answer.

CodePudding user response:

An Entry widget can only hold single line of string. Use Listbox widget instead. Also avoid using wildcard import and it is better to use pack() or grid() instead of place() in normal case.

Below is an example based on your code:

import tkinter as tk
from os import listdir

raiz = tk.Tk()

ruta = './imagenes'

fotos = tk.Frame(raiz, bd=10, relief="groove")
fotos.pack(fill="both", expand=1)

lblfotos = tk.Listbox(fotos, width=30, height=20)
lblfotos.pack(fill="both", expand=1)

for imgfile in listdir(ruta):
    lblfotos.insert("end", imgfile)

raiz.mainloop()
  •  Tags:  
  • Related