flet icon indicating copy to clipboard operation
flet copied to clipboard

Page keyboard event not changing

Open SKbarbon opened this issue 2 years ago • 2 comments

There is an issue with flet that page keyboard events is not changing even if I did update the page. Actually, it's changing, but the last function is still there. This is flet pip info:

Name: flet
Version: 0.5.2
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page: 
Author: Appveyor Systems Inc.
Author-email: [email protected]
License: Apache-2.0
Location: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages
Requires: flet-core, httpx, oauthlib, packaging, watchdog, websocket-client, websockets

And this is a code example that I did run:

from flet import *
import flet

def main (page:Page):
    def keyboard1 (e):
        print("I am keybpoard 1")
    
    def keyboard2 (e):
        print("I am keyboard 2")
    
    def change_page_keyboard_event (e):
        page.on_keyboard_event = keyboard2
        page.update()
    
    page.on_keyboard_event = keyboard1
    
    page.add(flet.TextButton("change page keyboard event", on_click=change_page_keyboard_event))


flet.app(target=main)

When I click "change page keyboard event" button, I expect that the function named keyboard1 is will not be called on any keyboard events. But the weird thing that they are called together !!

This is the output after calling change_page_keyboard_event function:

I am keybpoard 1
I am keyboard 2

How is that even possible 🙂!

SKbarbon avatar Apr 25 '23 11:04 SKbarbon

Unsubscribe first handler first, like this:

    def change_page_keyboard_event (e):
        page.on_keyboard_event.unsubscribe(keyboard1)
        page.on_keyboard_event = keyboard2
        page.update()

We should document it somewhere 🤗

FeodorFitsner avatar Apr 25 '23 16:04 FeodorFitsner

Ahh I see 😆! My bad

SKbarbon avatar Apr 25 '23 20:04 SKbarbon