【问题标题】:checkbox inline button in aiogram pythonaiogram python中的复选框内联按钮
【发布时间】:2022-01-08 23:44:14
【问题描述】:

我有这个图标内嵌按钮

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 的 call.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)

【问题讨论】:

    标签: python python-3.x async-await aiogram


    【解决方案1】:

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

    另外,还有aiogram-dialog 库。您可以在那里找到特殊的“复选框”按钮

    【讨论】:

      【解决方案2】:

      你可以试试:

      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,
      )
      

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 2023-01-17
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多