[[+content_image]]
N
N
Nickolay Tapokpy2021-12-02 02:48:43
Python
Nickolay Tapokpy, 2021-12-02 02:48:43

Dynamic creation of variables (InlineKeyboard), right?

from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
category_menu_list = [['1', 'Электроника'], ['2', 'Компьютеры и сети'], ['3', 'Бытовая техника'], ['4', 'Стройка и ремонт'], 
['5', 'Дом и сад'], ['6', 'Авто и мото'], ['7', 'Красота и спорт'], ['8', 'Детям и мамам'], ['9', 'Работа и офис'], ['16', 'Еда']]

for i, n in enumerate(category_menu_list, start=1):
    locals()["sub_cat_" + str(i)] = InlineKeyboardMarkup(row_width=1) # есть ли аналог  locals()
    for q, w in enumerate(all_items[n[1]]):
        name_button = w
        q = InlineKeyboardButton(text=name_button, callback_data=name_button)
        locals()["sub_cat_" + str(i)].insert(q)
    cancel_button = InlineKeyboardButton(text="Назад", callback_data="menu")
    locals()["sub_cat_" + str(i)].insert(cancel_button)

A snippet of code for creating a menu for a bot, can it be done differently, in a simpler way?
@dp.callback_query_handler(text=['Эле'])
async def show_sub_cat_1(call: CallbackQuery):
    await call.message.edit_reply_markup(reply_markup=sub_cat_1)

@dp.callback_query_handler(text=['Ком'])
async def show_sub_cat_2(call: CallbackQuery):
    await call.message.edit_reply_markup(reply_markup=sub_cat_2)

@dp.callback_query_handler(text=['Быт'])
async def show_sub_cat_3(call: CallbackQuery):
    await call.message.edit_reply_markup(reply_markup=sub_cat_3)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question