V
V
Vlad_Lop122021-04-25 19:11:28
Python
Vlad_Lop12, 2021-04-25 19:11:28

How to wrap inline buttons?

Hello!
I am writing a bot on aiogram and faced such a problem that I can’t transfer buttons.
That is, so that there are only 2 buttons on one level, and 2 on the other, and not so that they go in a row.

def  test():
    return types.InlineKeyboardMarkup(row_width = 2).row(
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test1')),
        types.InlineKeyboardButton('test' , callback_data=ge_cb.new(action='test2')),
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test3')),
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test4')))


I specify the row_width = 2 parameter, but nothing works.
Help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-04-25
@Vlad_Lop12

nothing works

Because the buttons are eventually added through the row method , which ignores the row_width
row() parameter, adds exactly the row, how many parameters were passed - as many buttons will be in the row (well, without exceeding the limits of the cart, of course)
add()adds buttons, breaking them into several lines along the row_width length
So replace with
def  test():
    return types.InlineKeyboardMarkup(row_width = 2).add(
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test1')),
        types.InlineKeyboardButton('test' , callback_data=ge_cb.new(action='test2')),
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test3')),
        types.InlineKeyboardButton('test', callback_data=ge_cb.new(action='test4')))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question