I
I
Irina2019-01-29 02:08:58
Python
Irina, 2019-01-29 02:08:58

How to nest 1 ,2 or 0 buttons in InlineKeyboard?

Friends, I have a function that, according to the conditions, should produce 1,2 or 0 buttons.
The question is how to return 0 buttons. I assumed it was like this:

else:
        key.add()

But apparently I'm wrong. Tell.
def make_batton_imper(message, ts, tts, add_buttons):
    key = telebot.types.InlineKeyboardMarkup(row_width=2) 
    data_but = "id_"+str(ts)
    but_i = telebot.types.InlineKeyboardButton(text=" + 1", callback_data=data_but)
    data_but_p = "id_"+str(tts)
    but_py = telebot.types.InlineKeyboardButton(text=" + 2", callback_data=data_but_p)
    if add_buttons == "pass":
         key.add(but_py)
    elif add_buttons == "all":
         key.row(but_i, but_py)
    else:
        key.add()
    return key

Or suggest another option?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Irina, 2019-01-31
@vera_ira

Solved the problem like this. Before starting the function, the script determines the number of required buttons and passes the data to the function to form the buttons. If the buttons are 0, then the function does not start.
If you know a shorter solution, please let me know. Thank you!

if ts != 0:
    if ts != 0 and tts != 0:
        key = make_batton_imper(message, ts, tts, add_buttons="all")
    elif ts != 0 and tts == 0:
        key = make_batton_imper(message, ts, tts, add_buttons="pass")
    answer = "бла-бла-бла с кнопками"
    bot.send_message(message.chat.id, answer, reply_markup=key, parse_mode='Markdown')
else:
    answer = "бла-бла-бла без кнопок" 
    bot.send_message_text(message.chat.id, answer, parse_mode='Markdown') 
        
def make_batton_imper(message, ts, tts, add_buttons):
    key = telebot.types.InlineKeyboardMarkup(row_width=2) 
    data_but = "id_"+str(ts)
    but_i = telebot.types.InlineKeyboardButton(text=" + 1", callback_data=data_but)
    data_but_p = "id_"+str(tts)
    but_py = telebot.types.InlineKeyboardButton(text=" + 2", callback_data=data_but_p)
    if add_buttons == "pass":
         key.add(but_py)
    elif add_buttons == "all":
         key.row(but_i, but_py)
    return key

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question