如何在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
相关产品推荐
相关产品推荐

