Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The event from child screen delivered to parent screen #5306

Closed
tsingwang opened this issue Nov 28, 2024 · 4 comments
Closed

The event from child screen delivered to parent screen #5306

tsingwang opened this issue Nov 28, 2024 · 4 comments

Comments

@tsingwang
Copy link

I recently upgraded textual, and found unexpected result with my app. Here is a sample test code.

Expect results: It should stop at parent screen when enter key pressed in child screen.

In textual==0.70.0, it worked, and the issue has existed since 0.71.0

from textual import events
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.screen import Screen
from textual.widgets import Static, Button


class DetailScreen(Screen):

    def compose(self) -> ComposeResult:
        yield Static('Detail ' + self.app.word, id="title")

    async def on_key(self, event: events.Key) -> None:
        if event.key == "enter":
            self.app.pop_screen()
            await self.app.next_word()


class TDictApp(App):

    def compose(self) -> ComposeResult:
        yield Vertical(Static(id="word"), classes="info")
        yield Button("Yes", id="yes", variant="success")
        yield Button("No", id="no", variant="error")

    async def on_mount(self) -> None:
        self.word = '0'
        await self.next_word()

    async def on_button_pressed(self, event: Button.Pressed) -> None:
        if self.word is None:
            return self.exit()

        self.push_screen(DetailScreen())

    async def next_word(self) -> None:
        self.word = str(int(self.word)+1)

        if self.word == '3':
            self.word = None
            self.query_one("#word").update("Well done! Hope to see you tomorrow :)")
            return

        self.query_one("#word").update(self.word)


if __name__ == "__main__":
    app = TDictApp()
    app.run()
@Textualize Textualize deleted a comment from github-actions bot Nov 28, 2024
@willmcgugan
Copy link
Collaborator

I'm afraid I don't follow what the issue is.

Can you provide step by step instructions on what to do.

And what happened in the older version.

We have a Chinese speaker in the office, if you would prefer to do this in Chinese.

@tsingwang
Copy link
Author

哈哈,好的,那我就用中文描述了。
这个代码是简单的,第一个Screen上有两个Button,点击后 push 另一个Screen,输入Enter键后 pop 第二个Screen并返回到第一个Screen等待用户输入,这样一直循环。

我测试发现 0.70.0 版本运行是符合上述描述的,但之后的版本有问题。

在新版本中,当在第二个Screen输入Enter键后,它返回到第一个Screen的瞬间又到了第二个Screen,没有停留在第一个Screen。我打印日志发现第二个Screen的Enter键触发到第一个Screen的on_button_pressed 方法了,我想这是不符合预期的。

EN:
This code is simple, there are two buttons on the first screen, click on it and push the second screen, enter the key and then pop the second screen and return to the first screen to wait for the user to type, and so on and so on.

I tested and found that version 0.70.0 runs as described above, but later versions have issues.

In the new version, when you enter the Enter key on the second screen, it returns to the first screen and then to the second screen, instead of staying on the first screen. I printed the log and found that the Enter key of the second screen triggered the on_button_pressed method of the first screen, which I think is not as expected.

You can run it locally and take a look, thanks.

@tsingwang
Copy link
Author

I have found the reason, we can stop the event to delivery.

In this case, we can add a line event.stop() in Child Screen on_key().

Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants