Перейти к содержимому

Навигация между экранами

Создайте экземпляр экрана и вызовите open() из обработчика:

profile = ProfileScreen()
settings = SettingsScreen()
bot.register_ui(profile, settings)
@bot.on_command("/profile")
async def cmd_profile(ctx: Context):
await profile.open(ctx)
@bot.on_command("/settings")
async def cmd_settings(ctx: Context):
await settings.open(ctx)

Внутри callback-обработчика вызовите open() другого экрана:

class MenuScreen(UIComponent):
__prefix__ = "menu"
def __init__(self, profile_screen, settings_screen):
self._profile = profile_screen
self._settings = settings_screen
@UIComponent.callback("go_profile")
async def on_profile(self, ctx: Context) -> None:
await ctx.answer_callback()
await self._profile.open(ctx)
@UIComponent.callback("go_settings")
async def on_settings(self, ctx: Context) -> None:
await ctx.answer_callback()
await self._settings.open(ctx)

Кнопка одного экрана может направлять callback на другой через абсолютный путь (начинается с /):

@keyboards.register("settings_main")
def kb_settings() -> InlineKeyboard:
kb = InlineKeyboard()
kb.button("🎨 Тема", callback_data="theme") # → "settings:theme"
kb.button("🔔 Уведомления", callback_data="notify") # → "settings:notify"
kb.row()
kb.button("👤 Профиль", callback_data="/profile:open") # → "profile:open"
kb.button("🏠 Меню", callback_data="/menu:home") # → "menu:home"
return kb

Используйте InlineKeyboard.extend() для добавления общих кнопок:

def navigation_footer() -> InlineKeyboard:
kb = InlineKeyboard()
kb.button("🏠 Меню", callback_data="/menu:home")
kb.button("👤 Профиль", callback_data="/profile:open")
return kb
@keyboards.register("settings_main")
def kb_settings() -> InlineKeyboard:
kb = InlineKeyboard()
kb.button("🎨 Тема", callback_data="theme")
kb.extend(navigation_footer())
return kb