在 Windows 10 计算机上使用 Kivy 2.3.0、Python 3.12。 启动应用程序时,我收到这样的错误消息(所有错误消息都在下面):
[ERROR ] unable to access to <\DumpStack.log.tmp>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
我相信这与 FileCooser 有关。 在我对这些文件进行过滤之前,它正在访问它无法处理的系统文件。在我的 kv 文件中,在 FileChooserListView 中:我进行了这样的设置:
FileChooserListView:
id: filechooser
filters: root.filters
filter_dirs: root.filter_dirs # Bind the filter function for directories
filter_func: root.filter_files # Bind the filter function here
dirselect: root.dirselect
multiselect: False
show_hidden: False # Hide hidden and system files
BoxLayout:
在该应用程序中,我设置了文件和目录过滤。 但我没有看到它们正在被访问,因为我在其中放入了打印语句。
class FileChooserScreen(Screen):
filters = ObjectProperty(['*'])
dirselect = BooleanProperty(False)
file_type = None
last_path = StringProperty("")
def filter_files(self, folder, filename):
print(f"filter_files called for: {filename}")
"""Custom filter to exclude specific system files and handle permission errors."""
system_files = ['hiberfil.sys', 'pagefile.sys', 'swapfile.sys', 'DumpStack.log.tmp']
# system_files = os.listdir(folder)
try:
# Exclude system files based on their name
if filename in system_files:
print(f"Excluding system file: {filename}")
return False
return True # Allow the file if it's not a system file
except (OSError, PermissionError) as e:
# Handle permission errors gracefully and skip the file
print(f"Error accessing {filename}: {e}")
return False # Exclude files that raise access errors
def filter_dirs(self, folder, filename):
"""Filter out system directories and handle permission errors."""
system_dirs = ['$Recycle.Bin', 'System Volume Information', 'Config.Msi', 'Windows', 'Program Files',
'Program Files (x86)']
try:
if filename in system_dirs or filename.startswith('$') or filename.startswith('.'):
print(f"Excluding system file: {filename}")
return False # Exclude system directories and hidden ones
return True # Include other directories
except (OSError, PermissionError) as e:
print(f"Error accessing directory {filename}: {e}")
return False # Exclude directories that raise access errors
我在 on_entry 中给他们打电话:
def on_enter(self):
# Set up filters for files and directories when the screen is entered
# print("Setting filters in on_enter for FileChooser")
# self.ids.filechooser.filter_func = self.filter_files
# self.ids.filechooser.filter_dirs = self.filter_dirs
# Set the last path or default to Documents folder
main_screen = self.manager.get_screen('main')
file_type_last_path = main_screen.last_paths.get(self.file_type, "")
self.ids.filechooser.path = file_type_last_path if file_type_last_path else get_documents_path()
我什至尝试在应用程序中更早地给他们打电话,我确实看到我的消息打印出来,说过滤器已设置,但没有运气相同的问题。 我注释掉了下面的 on_start,但也尝试了该代码。
class ImageDownloaderApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(HelpScreen(name='help'))
sm.add_widget(FileChooserScreen(name='filechooser'))
sm.add_widget(RunningScreen(name='running'))
return sm
def on_start(self):
# Access the filechooser screen and set filters after the app starts
file_chooser_screen = self.root.get_screen('filechooser')
# Set the filters for files and directories
file_chooser_screen.ids.filechooser.filter_func = file_chooser_screen.filter_files
file_chooser_screen.ids.filechooser.filter_dirs = file_chooser_screen.filter_dirs
# Optionally set default filters for file types (e.g., CSV, PDF)
# file_chooser_screen.ids.filechooser.filters = ['*.csv', '*.pdf']
file_chooser_screen.ids.filechooser.show_hidden = False # Hide hidden files by default
print("Filters and custom file filtering set after startup!")
# Load the KV file (this should stay as it is)
Builder.load_file('image_downloader.kv')
if __name__ == '__main__':
ImageDownloaderApp().run()
错误如下:
[INFO ] [Logger ] Record log in C:\Users\.....\.kivy\logs\kivy_24-10-10_20.txt
[INFO ] [deps ] Successfully imported "kivy_deps.angle" 0.4.0
[INFO ] [deps ] Successfully imported "kivy_deps.glew" 0.3.1
[INFO ] [deps ] Successfully imported "kivy_deps.sdl2" 0.7.0
[INFO ] [Kivy ] v2.3.0
[INFO ] [Kivy ] Installed at "C:\Git\.....\.venv\Lib\site-packages\kivy\__init__.py"
[INFO ] [Python ] v3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)]
[INFO ] [Python ] Interpreter at "C:\Git\.....\.venv\Scripts\python.exe"
[INFO ] [Logger ] Purge log fired. Processing...
[INFO ] [Logger ] Purge finished!
[INFO ] [Factory ] 195 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.6.0 - Build 31.0.101.2127'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) UHD Graphics 630'>
[INFO ] [GL ] OpenGL parsed version: 4, 6
[INFO ] [GL ] Shading version <b'4.60 - Build 31.0.101.2127'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [GL ] NPOT texture support is available
[INFO ] [Base ] Start application main loop
[ERROR ] unable to access to <\DumpStack.log.tmp>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\hiberfil.sys>
Traceback (most recent call last):
File "C:\Git\.....\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\pagefile.sys>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\swapfile.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\DumpStack.log.tmp>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\hiberfil.sys>
Traceback (most recent call last):
File "C:\Git\........\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\pagefile.sys>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] unable to access to <\swapfile.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\DumpStack.log.tmp>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\hiberfil.sys>
Traceback (most recent call last):
File "C:\Git\......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\pagefile.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\swapfile.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\DumpStack.log.tmp>
Traceback (most recent call last):
File "C:\Git\........\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\hiberfil.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\pagefile.sys>
Traceback (most recent call last):
File "C:\Git\........\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[ERROR ] [unable to access to <C]\swapfile.sys>
Traceback (most recent call last):
File "C:\Git\.......\.venv\Lib\site-packages\kivy\uix\filechooser.py", line 180, in is_hidden
return GetFileAttributesExW(fn)[0] & FILE_ATTRIBUTE_HIDDEN
^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.error: (32, 'GetFileAttributesEx', 'The process cannot access the file because it is being used by another process.')
[INFO ] [WindowSDL ] Window is now on display 1
[INFO ] [WindowSDL ] Window is now on display 0
我必须在下面的 FileChooserListView kv 文件设置中将“show_hidden: False”更改为“show_hidden: True”。 我相信 show_hidden 进程与我的其他过滤发生冲突,从而导致了问题。 我用 Kivy 代码示例、我的代码和其他示例复制了它。
我的代码中还有其他错误,但这解决了问题!
filter_dirs 是一个布尔属性
也没有“filters_func”这样的东西。 我发现我可以将列表传递给
filters: [root.filter_files] # 将过滤器函数包装在列表中
这是不好的:
FileChooserListView:
id: filechooser
filters: root.filters
filter_dirs: root.filter_dirs # Bind the filter function for
directories
filter_func: root.filter_files # Bind the filter function here
dirselect: root.dirselect
multiselect: False
show_hidden: False # Hide hidden and system files
和工作:
FileChooserListView:
id: filechooser
filters: [root.filter_files]
dirselect: root.dirselect # This returns a True/False direct select a folder?
multiselect: False
show_hidden: True # Hide hidden and system files
对我的代码进行了一些其他更改,包括此导入:
from fnmatch import fnmatch
class FileChooserScreen(Screen):
filters = ObjectProperty(['*']) # Default filter on, to let all files throught
dirselect = BooleanProperty(False) # Default to file selection mode
file_type = StringProperty('*') # Track the current file type filter
last_path = StringProperty("") # To store the last selected path
def filter_files(self, folder, filename):
"""Custom filter function to filter files based on type and exclude specific files."""
system_files = ['hiberfil.sys', 'pagefile.sys', 'swapfile.sys', 'DumpStack.log.tmp']
# Filter out system files
if filename in system_files:
return False
# Apply file type filtering based on the current filter setting
from fnmatch import fnmatch
if not any(fnmatch(filename, pattern) for pattern in self.filters):
return False
return True
def set_file_type(self, file_type):
# Set the file type (csv, pdf, folder) and adjust filters
self.file_type = file_type
if file_type == 'csv':
self.filters = ['*.csv']
self.dirselect = False
elif file_type == 'pdf':
self.filters = ['*.pdf']
self.dirselect = False
elif file_type == 'folder':
self.filters = ["*/"]
self.dirselect = True
else: # Added this catch all
self.filters = ['*'] # Show all files
# Update the visibility of the folder options in the UI
folder_options = self.ids.folder_options
if self.dirselect:
folder_options.opacity = 1 # Show folder creation options
else:
folder_options.opacity = 0 # Hide folder creation options