I can't find an issue with my code, please help me debug... can't describe any better sorry

Hi, I have this bit of code that is supposed to open the directory selector for the user to go to a directory and… select it. The code itself works on Linux only, and was given to me on r/learnpython.
The code - which is working by itself - :

import gi


gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


dialog = Gtk.FileChooserDialog(
    title="Select folder", parent=None, action=Gtk.FileChooserAction.SELECT_FOLDER
)
dialog.add_button("Cancel", Gtk.ResponseType.CANCEL)
dialog.add_button("Select", Gtk.ResponseType.OK)


response = dialog.run()
if response == Gtk.ResponseType.OK:
    directory = dialog.get_filename()
    print(directory)
dialog.destroy()

But when I put it in my actual app’s code, then it doesn’t work. The window opens, but clicking “select” will make the app freeze completely.

here is my code, in which I implemented the gtk code that was given to me.

import flet as ft
from flet_route import Params, Basket
import subprocess
import gi


gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


def parameters(page: ft.Page, params: Params, basket: Basket):


    def select_folder(self):
        dialog = Gtk.FileChooserDialog(
            title="Select folder", parent=None, action=Gtk.FileChooserAction.SELECT_FOLDER
        )
        dialog.add_button("Cancel", Gtk.ResponseType.CANCEL)
        dialog.add_button("Select", Gtk.ResponseType.OK)


        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            directory = dialog.get_filename()


        dialog.destroy()



    return ft.View(
        "/parameters/",
        controls=[
            ft.Row(
                controls=[
                    ft.IconButton(
                        icon=ft.icons.ARROW_BACK,
                        icon_size=20,
                        on_click=lambda _: page.go("/")
                    )
                ],
                alignment=ft.MainAxisAlignment.START
            ),
            ft.Container(  # Conteneur qui centre la colonne
                content=ft.Column(
                    controls=[
                        ft.ElevatedButton(
                            "Sélectionner un répertoire",
                            on_click=select_folder
                        ),
                    ],
                    alignment=ft.MainAxisAlignment.CENTER,
                    horizontal_alignment=ft.CrossAxisAlignment.CENTER
                ),
                alignment=ft.alignment.center,  # Centre tout le contenu
                expand=True  # Fait en sorte que le container prenne tout l'espace
            )
        ],
        vertical_alignment=ft.MainAxisAlignment.CENTER  # Centre tout le contenu verticalement
    )

Could someone help me debug please ? I really tried but I can’t find what the issue is. It’s my first time really trying coding, please be indulgent to possible obvious mistakes !

I am not a Gtk user so I do not know if your code is missing something, like an event loop.

I yout place I would have a look at the tutorial documentation.
Which is here The Python GTK+ 3 Tutorial — Python GTK+ 3 Tutorial 3.4 documentation

1 Like

The reason your app freezes after clicking “Select” is because Gtk.FileChooserDialog.run() is a blocking function. It stops the flow of your app and waits for the user to interact. That works fine in a simple script, but not in a GUI framework like Flet, which is async and expects non-blocking callbacks.

I don’t know of a simple way to fix this. Flet is meant to create desktop and web apps, so it doesn’t have a directory selector dialogue. However, you might want to look into Flet’s FilePicker widget?

Yes thanks, that’s what I’ll be doing. It’s more buggy than the gtk method, but I’ve learned that mixing flet and gtk is not a good idea !