复选框内联按钮在aiogram python



我有这个图标内嵌按钮

from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from middlewares.internationlization import _
choise_sign_select_company = InlineKeyboardMarkup(row_width=1,
inline_keyboard=[
[
InlineKeyboardButton(
text=_('Капитализация ❌'),
callback_data='market_cap_of_company'
),
InlineKeyboardButton(
text=_('Кол-во акций ❌'),
callback_data='count_shares_ofustanding_of_company'
)
],
[
InlineKeyboardButton(
text=_('Цена акции ❌'),
callback_data='current_prise_share_now_of_company'
)
]
])

如何把这些按钮变成一个按钮的检查?例如,当你点击&;大写❌&;按钮,按钮变为"大写✅"(当您再次单击它时,它会返回)
并且该值也存储在调用中。callback_data

中的数据键盘来了

@dp.message_handler(text='Подобрать компании 🤑')
async def select_sign(message: types.Message):
await message.answer(_('Выберите признаки на которых будет основываться подбор'), 
reply_markup=choise_sign_select_company)
await SelectCompanies.enter_the_sign.set()

点击按钮这里处理

@dp.callback_query_handler(state=SelectCompanies.enter_the_sign)
async def select_interval(call: types.CallbackQuery):
text = call.data
await call.answer(text)

您可以使用回调手动实现此功能。
例如,在回调中存储当前按钮的状态,并在单击后更改它。

还有一个aiogram-dialog库。您可以找到特殊的"复选框"。按钮

你可以试试:

from aiogram_dialog import DialogManager, ChatEvent
from aiogram_dialog.widgets.kbd import Checkbox, ManagedCheckboxAdapter
from aiogram_dialog.widgets.text import Const

async def check_changed(event: ChatEvent, checkbox: ManagedCheckboxAdapter, manager: DialogManager):
print("Check status changed:", checkbox.is_checked())

check = Checkbox(
Const("✓  Checked"),
Const("Unchecked"),
id="check",
default=True,  # so it will be checked by default,
on_state_changed=check_changed,
)

最新更新