我從 tkinter 開始,我已經生成了一個包含某個檔案夾中元素的串列,我需要在界面中顯示它們。我把它放在標簽中,但它水平顯示元素,我需要它顯示一個在另一個下方,有沒有辦法做到這一點?
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]:PS 原意是檔案夾中的檔案顯示在界面中,可以進行互動,比如打開或者洗掉,但是沒找到,在tkinter里面可以嗎?或者也許在另一個圖書館?謝謝您的回答。
uj5u.com熱心網友回復:
一個Entry小部件只能保存單行字串。請改用Listbox小部件。還要避免使用通配符匯入,在正常情況下最好使用pack()orgrid()代替。place()
以下是基于您的代碼的示例:
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411584.html
標籤:
