skip to Main Content

I need to write a function that creates an inline-keyboard. The function parameter is an integer, for example i.

Number of buttons in the keyboard: i + 1.
Condition number of buttons per line: <=3.

2

Answers


  1. Chosen as BEST ANSWER

    I found this variant:

    def get_kb(number_of_buttons):
        markup = InlineKeyboardMarkup()
        lines = int(number_of_buttons/3)+1
        last_lines = (number_of_buttons % 3)
        for i in range(1, lines):
            markup.add(InlineKeyboardButton('button ' + str(i ** i), callback_data='button_' + str(i ** i)),
                       InlineKeyboardButton('button ' + str(i ** i + 1), callback_data='button_' + str(i ** i + 1)),
                       InlineKeyboardButton('button ' + str(i ** i + 2), callback_data='button ' + str(i ** i + 2)))
        if last_lines == 0:
            markup.add(InlineKeyboardButton('others', callback_data='others'))
        elif last_lines == 1:
            markup.add(InlineKeyboardButton('button ' + str(q), callback_data='button ' + str(q)),
                       InlineKeyboardButton('others', callback_data='others'))
        else:
            markup.add(InlineKeyboardButton('button ' + str(q-1), callback_data='button ' + str(q-1)),
                       InlineKeyboardButton('button ' + str(q), callback_data='button ' + str(q)),
                       InlineKeyboardButton('others', callback_data='others'))
    

    But I think that isn't the best variant


  2. Based on your own answer and since you mentioned aiogram in your comment, there’s already a feature implemented inside the library for your use case:

    # number_of_buttons_in_row would be 3 in your case
    def get_kb(number_of_buttons, number_of_buttons_in_row):
        markup = InlineKeyboardMarkup()    
        markup.row_width = number_of_buttons_in_row     # key part is here
    
        for i in range(1, number_of_buttons):
            markup.add(InlineKeyboardButton('button ' + str(i ** i), callback_data='button_' + str(i ** i)),
                       InlineKeyboardButton('button ' + str(i ** i + 1), callback_data='button_' + str(i ** i + 1)),
                       InlineKeyboardButton('button ' + str(i ** i + 2), callback_data='button ' + str(i ** i + 2)))
    
        markup.add(InlineKeyboardButton('others', callback_data='others'))
        markup.add(InlineKeyboardButton('button ' + str(q), callback_data='button ' + str(q)),
                   InlineKeyboardButton('others', callback_data='others'))
    
        markup.add(InlineKeyboardButton('button ' + str(q-1), callback_data='button ' + str(q-1)),
                   InlineKeyboardButton('button ' + str(q), callback_data='button ' + str(q)),
                   InlineKeyboardButton('others', callback_data='others'))
        return markup
    
    

    As you can see there’s a field called row_width inside markup, that’s used to slice the buttons into the given width and place them in several lines

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search