You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python函数中根据单元格值动态设置InlineKeyboardButton名称

问题:根据单元格值设置按钮文本为Pending/Verified

我需要实现一个函数,根据单元格中的值(1或0),将按钮名称设置为PENDING或VERIFIED。

我尝试了这段代码,单独运行是有效的:

fstring = f"{'Pending' if df.Sold[payment_verified(order).index[0]] == 0 else 'Verified'}"

但把它放到下面的函数里就无法正常工作:

def gen_verifymarkup(chatid):
    verifymarkup = InlineKeyboardMarkup()
    verifymarkup.row_width = 2
    my_orders = check_my_orders(chatid)
    for order in my_orders:
        try:
            fstring = ['Pending' if (df.Sold[payment_verified(order).index][0] == 0) else 'Verified']
            verifymarkup.add(InlineKeyboardButton(f"{order}", callback_data=f"cb_verify-{order}"),
            InlineKeyboardButton(f"{fstring}",callback_data=f"cb_verified-{order}"))
        except Exception as e:
            print(e)
            
        print(order)
    return verifymarkup 

感谢帮助!


更新:问题已解决

修改后的代码如下:

for order in my_orders:
    try:
        if list(payment_verified2(order)["Sold"].values())[0] == 1:
            print(order)
            verifymarkup.add(
                InlineKeyboardButton(f"{order}", callback_data=f"cb_verify-{order}"),
                InlineKeyboardButton("Verified", callback_data=f"cb_verified-{order}"),
            )
        elif list(payment_verified2(order)["Sold"].values())[0] != 1:
            verifymarkup.add(
                InlineKeyboardButton(f"{order}", callback_data=f"cb_verify-{order}"),
                InlineKeyboardButton("Pending", callback_data=f"cb_verified-{order}"),
            )
            print("fail")
    except:
        verifymarkup.add(
            InlineKeyboardButton(f"{order}", callback_data=f"cb_verify-{order}"),
            InlineKeyboardButton("Pending", callback_data=f"cb_verified-{order}"),
        )
        continue

内容的提问来源于stack exchange,提问作者Norman Crypt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 19:18:20