Python库aiogram.如何同时添加多个内联按钮



如何一次添加多个内联按钮,这样就不会为每个按钮编写单独的代码行,同时在每行中显示三个按钮。我试过这个代码,但我得到了这个:代码:

import logging
from aiogram import Bot, Dispatcher, executor, types
import csv
from aiogram.dispatcher.filters import filters, Text
from aiogram.types import ReplyKeyboardMarkup, InlineKeyboardMarkup, KeyboardButton, WebAppInfo, KeyboardButtonPollType, 
InlineKeyboardButton, ReplyKeyboardRemove
API_TOKEN = '5539016910:AAE0QLol57MMWA65-G0dxu-F69exdXlS-B4'
logging.basicConfig(level=logging.INFO)
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
def read_csv(file_name):
file = open(file_name)
file.readline()
return csv.reader(file)

regions = {name: _id for _id, name in read_csv('regions.csv')}
districts = {_id: {name: reg_id} for _id, name, reg_id in read_csv('districts.csv')}

@dp.message_handler(commands=['start'])
async def region(message: types.Message):
url8 = 'https://docs.aiogram.dev/en/latest/quick_start.html'
rkm = InlineKeyboardMarkup(row_wigth=3)
l = list(regions.keys())
for i in range(len(l)):
rkm.row(InlineKeyboardButton(l[i], url=url8))
await message.answer("Regions of Uzbekistan:", reply_markup=rkm)

在此处输入图像描述

传递给InlineKeyboardMarkup构造函数的参数名称中有一个拼写错误。您还应该执行.add,而不是.row

rkm = InlineKeyboardMarkup(row_width=3)
l = list(regions.keys())
for i in range(len(l)):
rkm.add(InlineKeyboardButton(l[i], url=url8))

最新更新