Telegram Bot中InlineKeyboardMarkup实现垂直时间按钮求助
解决Telegram Bot时间按钮垂直排列的问题
方案一:修改原有代码(无需额外库)
原代码把所有时间按钮塞进同一个行数组,导致按钮横向排列。调整循环逻辑,让每个时间按钮单独成为一行即可:
import json def create_callback_data_time(action, time): """ Create the callback data associated to each button""" return ";".join([action, str(time)]) def create_time(): markup_time = {"inline_keyboard": []} # 每个时间单独生成一行按钮 for time in ["10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00"]: # 每次循环创建仅包含当前时间按钮的行 single_button_row = [{"text": time, "callback_data": create_callback_data_time("TIME", time)}] markup_time["inline_keyboard"].append(single_button_row) return json.dumps(markup_time)
方案二:正确使用Keyboa库
你之前用Keyboa未成功,是因为没指定每行显示的按钮数量。添加items_per_row=1参数,强制每行只显示一个按钮:
from keyboa import Keyboa getting_list_of_services = core.check_service() # 如果check_service()返回的是时间字符串列表,直接传入即可,无需额外转换 keyboard = Keyboa(items=getting_list_of_services, front_marker="TIME", back_marker="", items_per_row=1).items return keyboard
内容的提问来源于stack exchange,提问作者iozbsdvs
相关产品推荐
相关产品推荐

