python-3.x - 在 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 新手,现在正在学习。感谢您的时间。
解决方案
推荐阅读
- azure - Microsft Graph API 作为 WEB API 应用程序
- c++ - C++ 正则表达式搜索特定的中文模式
- laravel - 可搜索的 Laravel Yajra 数据表不适用于欲望列
- python - heroku 上的空数据框但不是本地的
- java - Java:循环中的 String.replaceAll() 与 matcher.replaceAll()
- sql-server - 替代 SQL Server 链接服务器
- python - Open Data Cube:无法连接到 opendatacube Conda 环境中的数据库
- python - 提高从函数创建多维数组的效率
- list - 在 SwiftUI 中更新任何 @State/@Binding 变量时,所有元素都会重新渲染
- css - 用于列的 Antd 响应式 Flex