Answer the question
In order to leave comments, you need to log in
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()
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
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question