用wxPython实现获取当前文件夹中所有图片文件列表
时间: 2024-02-28 11:56:36 浏览: 107
要获取当前文件夹中所有图片文件的列表,你可以使用 os 模块和 wxPython 的 wx.ListBox 控件。具体实现步骤如下:
1. 首先,使用 os 模块获取当前文件夹中所有文件的列表:
```python
import os
path = "./" # 当前文件夹路径
files = os.listdir(path) # 获取当前文件夹中所有文件的列表
```
2. 然后,遍历文件列表,筛选出图片文件:
```python
image_extensions = [".jpg", ".jpeg", ".png", ".bmp"] # 支持的图片格式列表
image_files = [] # 存储图片文件的列表
for file in files:
if os.path.isfile(os.path.join(path, file)):
ext = os.path.splitext(file)[1].lower() # 获取文件扩展名
if ext in image_extensions:
image_files.append(file)
```
3. 最后,将图片文件列表显示在 wx.ListBox 控件中:
```python
import wx
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="Image File List", size=(300, 300))
# 获取图片文件列表
path = "./"
files = os.listdir(path)
image_extensions = [".jpg", ".jpeg", ".png", ".bmp"]
image_files = []
for file in files:
if os.path.isfile(os.path.join(path, file)):
ext = os.path.splitext(file)[1].lower()
if ext in image_extensions:
image_files.append(file)
# 创建 wx.ListBox 控件,并将图片文件列表设置为它的内容
self.list_box = wx.ListBox(self, wx.ID_ANY, choices=image_files)
# 显示窗口
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MyFrame()
app.MainLoop()
```
注意:在实际使用中,你需要根据你的具体需求来改变代码,比如设置文件夹路径、改变支持的图片格式等。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20210720083447.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)