Strange behavior when adding SystemCommand
to switch screen modes
#6118
-
I tried adding a from typing import Iterable
from textual.app import App, ComposeResult, SystemCommand
from textual.screen import Screen
from textual.widgets import (
Footer,
Header,
Placeholder,
)
class SettingScreen(Screen):
BINDINGS = [("esc", "app.switch_mode('main')", "Exit")]
def compose(self) -> ComposeResult:
yield Header()
yield Placeholder("Setting Screen")
yield Footer()
class MainScreen(Screen):
def compose(self) -> ComposeResult:
yield Header()
yield Placeholder("Main Screen")
yield Footer()
class FooApp(App):
BINDINGS = [
("q", "quit", "Quit"),
]
MODES = {
"main": MainScreen,
"setting": SettingScreen,
}
async def on_mount(self) -> None:
await self.switch_mode("main")
def get_system_commands(self, screen: Screen) -> Iterable[SystemCommand]:
yield from super().get_system_commands(screen)
yield SystemCommand("Setting", "App Setting", self.switch_mode("setting"))
if __name__ == "__main__":
app = FooApp()
app.run() Crash Info:
Textual DiagnosticsVersions
Python
Operating System
Terminal
Rich Console options
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
We found the following entry in the FAQ which you may find helpful: Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review. This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others). This is an automated reply, generated by FAQtory |
Beta Was this translation helpful? Give feedback.
-
Have a look at the following line again: yield SystemCommand("Setting", "App Setting", self.switch_mode("setting")) The third argument expects a callback, but you are calling What you want is to supply a callable so the command palette can call it when it is selected. In the examples you linked to, you will notice that the third parameter is Given that your callback has a parameter, you could replace the call with a partial or a lambda. Or define a new method. For example: def switch_settings_mode(self):
self.switch_mode("setting") yield SystemCommand("Setting", "App Setting", self.switch_settings_mode) |
Beta Was this translation helpful? Give feedback.
Have a look at the following line again:
The third argument expects a callback, but you are calling
switch_mode
. So the switch happens right there in that method.What you want is to supply a callable so the command palette can call it when it is selected. In the examples you linked to, you will notice that the third parameter is
self.bell
, and notself.bell()
.Given that your callback has a parameter, you could replace the call with a partial or a lambda. Or define a new method. For example: