简体   繁体   English

如何在 Tkinter 上显示垂直列表

[英]How show vertical list on Tkinter

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.我从 tkinter 开始,我已经生成了一个包含某个文件夹中元素的列表,我需要在界面中显示它们。 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?我把它放在 label 中,但它水平显示元素,我需要它显示一个在另一个下方,有没有办法做到这一点?

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 https://i.stack.imgur.com/JaEek.png

[1]: PS 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? [1]:PS 原意是文件夹中的文件显示在界面中,可以进行交互,比如打开或者删除,但是没找到怎么做,tkinter能做到吗? or maybe in another library?.或者也许在另一个图书馆? Thank you for your answer.谢谢您的回答。

An Entry widget can only hold single line of string.一个Entry小部件只能保存单行字符串。 Use Text widget instead.请改用Text小部件。 Also avoid using wildcard import and it is better to use pack() or grid() instead of place() in normal case.还要避免使用通配符导入,在正常情况下最好使用pack()grid()而不是place()

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", width=500, height=200)
fotos.pack(fill="both", expand=1)

lblfotos = tk.Text(fotos)
lblfotos.pack(fill="both", expand=1)

lblfotos.insert("end", "\n".join(listdir(ruta)))

raiz.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM