【发布时间】:2021-09-20 04:45:06
【问题描述】:
我可以完美地使用第一个 inlinekeyboard(self._morebuttons1) 中的每个按钮,并且可以从第一个中打开第二个 inlinekeyboard(self._morebuttons2),但没有一个按钮可以在第二个 inlinekeyboard 上使用,可能是因为 callback_data。我试图回到函数的开头,但总是得到错误NameError: name '_morebuttons1' is not defined。
如何使用两个或多个内联键盘?
我的代码示例:
handles = [
CommandHandler('One', self._one),
CommandHandler('morebuttons2', self._morebuttons2),
CommandHandler('Three', self._three),
CommandHandler('morebuttons1', self._morebuttons1),
]
callbacks = [
CallbackQueryHandler(self._morebuttons1_inline),
CallbackQueryHandler(self._morebuttons2_inline),
def _morebuttons1(self, update: Update, context: CallbackContext) -> None:
keyb = [[InlineKeyboardButton('One', callback_data='1')],
[InlineKeyboardButton('To the second inlinekeyboard', callback_data='2')]]
menu_choices = InlineKeyboardMarkup(keyb)
update.message.reply_text("start with commands 1", reply_markup=menu_choices)
for handle in handles:
self._updater.dispatcher.add_handler(handle)
for callback in callbacks:
self._updater.dispatcher.add_handler(callback)
def _morebuttons_inline(self, update: Update, context: CallbackContext) -> None:
if update.callback_query:
msg = update.callback_query
if msg.data == '1':
self._one(update, context),
if msg.data == '2':
self._morebuttons2(update, context)
return _morebuttons1
def _morebuttons2(self, update: Update, context: CallbackContext) -> None:
keyb = [[InlineKeyboardButton('Three', callback_data='3')],
[InlineKeyboardButton('Back to the first inlinekeyboard', callback_data='4')]]
menu_choices = InlineKeyboardMarkup(keyb)
update.callback_query.message.edit_text("start with commands 2", reply_markup=menu_choices)
def _morebuttons2_inline(self, update: Update, context: CallbackContext) -> None:
if update.callback_query:
msg = update.callback_query
if msg.data == '3':
self._three(update, context),
if msg.data == '4':
self._morebuttons1(update, context)
return _morebuttons2
完整的追溯:
Traceback (most recent call last):
File "/home/jamreg/TelegramBotTest/glc/tg1.py", line 69, in wrapper
return command_handler(self, *args, **kwargs)
File "/home/jamreg/TelegramBotTest/glc/tg1.py", line 1058, in _morebuttons1_inline
return _morebuttons1
NameError: name '_morebuttons1' is not defined
谢谢! :)
【问题讨论】:
-
你能显示完整的回溯吗?此外,查看可运行的示例而不仅仅是回调也会很有帮助……
-
我添加了从 CommandHadlers 到 Inlinekeyboards 的代码的完整回溯。 :) @CallMeStag
-
我没有看到回溯。回溯是发生异常时打印的大型文本博客。它告诉你它发生在哪里。代码:请阅读telegra.ph/Minimal-Working-Example-for-PTB-07-18
-
我添加了回溯
-
啊。我应该是
return self._morebuttons1而不是return _morebuttons1。但如果没有 MWE,我无法确定。
标签: python telegram telegram-bot python-telegram-bot