首页 > 解决方案 > 在 Tkinter 列表框中显示文本

问题描述

我在 python3 中有一个小脚本,我想在 Tkinter 条目中输入路径,然后按下 Tkinter 按钮,使用“os.walk()”方法在列表框中显示路径的内容. 我不知道如何在列表框中显示“步行”的打印。这是我的代码:

import os
from tkinter import *
import re


class TKWindow:
    def __init__(self, window):
        self.window = window
        self.window.wm_title("List of folders , subfolders and files.")

        line1 = Label(new_window, text='Enter path to explore:')
        line1.grid(row=0, column=1)

        self.title = StringVar()
        self.entry1 = Entry(new_window, textvariable=self.title, width=30)
        self.entry1.grid(row=0, column=2)

        button1 = Button(new_window, text='Show Contents', width=15, command=lambda: self.populate_list_box())
        button1.grid(row=0, column=3)

        self.content_list = Listbox(new_window, height=25, width=69)
        self.content_list.grid(row=2, column=1, rowspan=5, columnspan=5)

        scrollbar1 = Scrollbar(new_window)
        scrollbar1.grid(row=2, column=6, rowspan=6)

        self.content_list.configure(yscrollcommand=scrollbar1.set)
        scrollbar1.configure(command=self.content_list.yview())

        self.content_list.bind(self.show_contents_of_folder)

    def show_contents_of_folder(self):
        path = self.retrieve_input()
        list_of_contents = []
        for foldername, subfolders, filenames in os.walk(path):
            list_of_contents.append('Current Folder:' + foldername)

            for subfolder in subfolders:
                list_of_contents.append('Subfolder of:' + foldername + ':' + subfolder)

            for filename in filenames:
                list_of_contents.append('Files inside:' + foldername + ':' + filename)
        return list_of_contents

    def retrieve_input(self):
        input_value = self.entry1.get()
        path_re = re.compile(r'.\\')
        path_re.sub('\\\\', input_value)
        return input_value

    def populate_list_box(self):
        lst = self.show_contents_of_folder()
        for i in lst:
            self.content_list.insert(i, END)

这是我得到的错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Downloads\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__return self.func(*args)
File "D:/Downloads/Python Projects/Organising_Files/Walking_a_directory_with_OS_module.py", line 18, in <lambda>
button1 = Button(new_window, text='Show Contents', width=15, command=lambda: self.populate_list_box())
File "D:/Downloads/Python Projects/Organising_Files/Walking_a_directory_with_OS_module.py", line 54, in populate_list_box self.content_list.insert(i, END)
File "D:\Downloads\Python\Python37\lib\tkinter\__init__.py", line 2806, in insert
    self.tk.call((self._w, 'insert', index) + elements)
_tkinter.TclError: bad listbox index "Current Folder:D:\Downloads\Word PDF files\Programming": must be active, anchor, end, @x,y, or a number

如果这是一个常见问题,我很抱歉,但我找不到适合我的答案,我是 python 新手,现在正在学习。感谢您的时间。

标签: python-3.xtkinter

解决方案


如下更新您的 populate_list_box 以将数据插入到您的列表框中:

def populate_list_box(self):
    lst = self.show_contents_of_folder()
    self.content_list.insert(END, *lst)

在此处输入图像描述


推荐阅读