flet icon indicating copy to clipboard operation
flet copied to clipboard

when starting the second page, an error appears "ValueError: signal only works in main thread of the main interpreter" in flet 0.24.1

Open CattyRu opened this issue 2 years ago • 2 comments

Duplicate Check

  • [x] I have searched the opened issues and there are no duplicates

Describe the bug

I am making an application in which I need to open the second page to enter parameters. I would not like to do this with the help of a dialog. In flet version 0.19.0, the code below worked, but there were problems with updating the size of the elements at the same time as updating the page size (with a certain assembly of elements, the first update of their sizes was applied only with the second update). In version 0.24.1, this problem has been solved, but I cannot open the second page at the same time as the first one. In the newest version (0.25.0.dev3529), this also does not work.

Code sample

Code
import flet as ft


def second_page(page: ft.Page):
    page.add(
        ft.Text('the test was successful')
    )


def first_page(page: ft.Page):
    page.add(
        ft.Text('Hi:)'),
        ft.TextButton(
            text='Start test',
            on_click=lambda e: ft.app(target=second_page)
        )
    )


ft.app(target=first_page)
here is a minimal reproducible example

To reproduce

Run the code on version 0.24.1 or higher. Below is the error report: Future exception was never retrieved future: <Future finished exception=ValueError('signal only works in main thread of the main interpreter')> Traceback (most recent call last): File "...\Local\Programs\Python\Python312\Lib\concurrent\futures\thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "...\.venv\Lib\site-packages\flet_core\page.py", line 942, in wrapper handler(*args) File "...\test.py", line 15, in <lambda> on_click=lambda e: ft.app(target=second_page) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "...\.venv\Lib\site-packages\flet\app.py", line 51, in app return asyncio.run( ^^^^^^^^^^^^ File "...\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "...\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "...\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 684, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "...\.venv\Lib\site-packages\flet\app.py", line 133, in app_async signal.signal(signal.SIGINT, exit_gracefully) File "...\Local\Programs\Python\Python312\Lib\signal.py", line 56, in signal handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: signal only works in main thread of the main interpreter

Expected behavior

The second page with the text "the test was successful" was expected to open

Screenshots / Videos

Captures flet 0.19.0:

image

Operating System

Windows

Operating system details

Windows 10 Pro

Flet version

0.24.1

Regression

Yes, it used to work in a previous Flet version (please specify the version in additional details)

Suggestions

in version 0.19.0, when calling ft.app, the __app_sync function was used for my application. Is it possible to provide a similar feature in new versions?

Logs

Logs
[Paste your logs here]

Additional details

No response

CattyRu avatar Oct 16 '24 11:10 CattyRu

In Flet, you can only display one page at a time but you can try this code


import flet as ft


def second_page(page: ft.Page):
    page.add(
        ft.Text('the test was successful')
    )


def first_page(page: ft.Page):
    page.add(
        ft.Text('Hi:)'),
        ft.TextButton(
            text='Start test',
            on_click=lambda e: switch_to_second_page(page)
        )
    )


def switch_to_second_page(page: ft.Page):
    page.clean()
    second_page(page)


ft.app(target=first_page)

OmarBelfeki avatar Oct 16 '24 14:10 OmarBelfeki

Thanks, but I need the first page to stay the same as in earlier versions

CattyRu avatar Oct 17 '24 06:10 CattyRu