Python 3.12, Flet 0.28.3. ハードコードされたルートディレクトリから、そのサブディレクトリと画像をCardとして一覧表示するコードを書いて。…
- User
Python 3.12, Flet 0.28.3. ハードコードされたルートディレクトリから、そのサブディレクトリと画像をCardとして一覧表示するコードを書いて。 Cardからはサブディレクトリに移動できるようにして、画像は開くことができるように。 ルートディレクトリ以外にいるときは戻るボタンで前に戻れる機能と、あとはルートまで一気に戻るボタンもつけて。 Cardは一列ではなくレスポンシブに複数列・複数行で表示して。
Assistant
以下は、Fletを使用して画像ブラウザを作成するコードです:
import flet as ft import os from pathlib import Path import subprocess import platform class ImageBrowser: def __init__(self): # ハードコードされたルートディレクトリ(必要に応じて変更してください) self.root_dir = Path("/Users/username/Pictures") # 例: MacOS # self.root_dir = Path("C:/Users/username/Pictures") # 例: Windows # self.root_dir = Path("/home/username/Pictures") # 例: Linux self.current_dir = self.root_dir self.history = [] # サポートする画像拡張子 self.image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp'} def is_image(self, path): return path.suffix.lower() in self.image_extensions def open_image(self, image_path): """OSのデフォルトアプリケーションで画像を開く""" system = platform.system() if system == 'Darwin': # macOS subprocess.run(['open', str(image_path)]) elif system == 'Windows': os.startfile(str(image_path)) else: # Linux subprocess.run(['xdg-open', str(image_path)]) def get_items(self): """現在のディレクトリのサブディレクトリと画像を取得""" items = [] try: for item in sorted(self.current_dir.iterdir()): if item.is_dir() and not item.name.startswith('.'): items.append(('dir', item)) elif item.is_file() and self.is_image(item): items.append(('image', item)) except PermissionError: pass return items def navigate_to(self, path): """指定されたパスに移動""" if path.is_dir(): self.history.append(self.current_dir) self.current_dir = path def go_back(self): """前のディレクトリに戻る""" if self.history: self.current_dir = self.history.pop() def go_to_root(self): """ルートディレクトリに戻る""" self.history.clear() self.current_dir = self.root_dir def main(page: ft.Page): page.title = "画像ブラウザ" page.padding = 20 page.window.width = 1200 page.window.height = 800 browser = ImageBrowser() # パスを表示するテキスト path_text = ft.Text( value=str(browser.current_dir), size=16, weight=ft.FontWeight.BOLD ) # カードを表示するグリッド grid = ft.GridView( expand=True, runs_count=5, # 列数(画面幅に応じて自動調整される) max_extent=200, # カードの最大幅 child_aspect_ratio=1.0, # カードのアスペクト比 spacing=10, run_spacing=10, ) def update_view(): """ビューを更新""" path_text.value = str(browser.current_dir) grid.controls.clear() items = browser.get_items() for item_type, item_path in items: if item_type == 'dir': # ディレクトリカード card = ft.Card( content=ft.Container( content=ft.Column([ ft.Icon( ft.icons.FOLDER, size=60, color=ft.colors.BLUE_400 ), ft.Text( item_path.name, size=14, text_align=ft.TextAlign.CENTER, overflow=ft.TextOverflow.ELLIPSIS ) ], alignment=ft.MainAxisAlignment.CENTER, horizontal_alignment=ft.CrossAxisAlignment.CENTER, spacing=10 ), padding=10, on_click=lambda e, p=item_path: navigate_to_dir(p) ), elevation=2 ) else: # 画像カード card = ft.Card( content=ft.Container( content=ft.Column([ ft.Icon( ft.icons.IMAGE, size=60, color=ft.colors.GREEN_400 ), ft.Text( item_path.name, size=14, text_align=ft.TextAlign.CENTER, overflow=ft.TextOverflow.ELLIPSIS ) ], alignment=ft.MainAxisAlignment.CENTER, horizontal_alignment=ft.CrossAxisAlignment.CENTER, spacing=10 ), padding=10, on_click=lambda e, p=item_path: open_image(p) ), elevation=2 ) grid.controls.append(card) # ナビゲーションボタンの表示/非表示 back_button.visible = browser.current_dir != browser.root_dir root_button.visible = browser.current_dir != browser.root_dir page.update() def navigate_to_dir(path): """ディレクトリに移動""" browser.navigate_to(path) update_view() def open_image(path): """画像を開く""" browser.open_image(path) def go_back(e): """前に戻る""" browser.go_back() update_view() def go_to_root(e): """ルートに戻る""" browser.go_to_root() update_view() # ナビゲーションボタン back_button = ft.IconButton( icon=ft.icons.ARROW_BACK, on_click=go_back, tooltip="前に戻る", visible=False ) root_button = ft.IconButton( icon=ft.icons.HOME, on_click=go_to_root, tooltip="ルートに戻る", visible=False ) # ヘッダー header = ft.Row([ back_button, root_button, path_text ], spacing=10) # メインレイアウト page.add( ft.Column([ header, ft.Divider(), grid ], expand=True) ) # 初期表示 update_view() ft.app(target=main)このコードの主な機能:
-
ルートディレクトリの設定:
ImageBrowserクラスの__init__メソッドで、ハードコードされたルートディレクトリを設定します。OSに応じて適切なパスに変更してください。 -
レスポンシブなグリッド表示:
GridViewを使用して、カードを複数列・複数行で表示します。画面サイズに応じて自動的に列数が調整されます。 -
ディレクトリナビゲーション: フォルダのカードをクリックすると、そのディレクトリに移動します。
-
画像を開く: 画像のカードをクリックすると、OSのデフォルトアプリケーションで画像が開きます。
-
ナビゲーションボタン:
- 戻るボタン(←): 前のディレクトリに戻ります
- ホームボタン(🏠): ルートディレクトリに一気に戻ります
- これらのボタンは、ルートディレクトリ以外にいるときのみ表示されます
-
現在のパス表示: 画面上部に現在のディレクトリパスが表示されます。
使用前に、
self.root_dirを実際の画像フォルダのパスに変更してください。また、必要に応じてimage_extensionsセットに対応する画像形式を追加できます。-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.