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

Pycord中View类按钮仅显示最后一个的原因咨询

问题原因及解决方法

核心问题:重复的回调方法名

你所有按钮的回调方法都命名为button_callback,在Python类中,后续定义的同名方法会覆盖之前的方法。Discord.py在注册按钮时,只会识别最后一个button_callback对应的按钮,所以最终只显示最后一个按钮。

额外问题:响应方式错误

代码中await interaction.response("Generating again...")是错误写法,正确的响应逻辑应该是:

  • 如果不需要延迟响应,直接使用await interaction.response.send_message("Generating again...")
  • 如果调用了defer(),需要用await interaction.followup.send("Generating again...")发送后续消息

修正后的代码

class genagain(discord.ui.View):

    @discord.ui.button(label="U1", style=discord.ButtonStyle.primary, custom_id="u1", row=0)
    async def u1_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
        
    @discord.ui.button(label="U2", style=discord.ButtonStyle.primary, custom_id="u2", row=0)
    async def u2_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
        
    @discord.ui.button(label="U3", style=discord.ButtonStyle.primary, custom_id="u3", row=0)
    async def u3_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
        
    @discord.ui.button(label="U4", style=discord.ButtonStyle.primary, custom_id="u4", row=0)
    async def u4_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
    
    @discord.ui.button(label="", style=discord.ButtonStyle.primary, emoji="🔄", custom_id="genagain", row=1)
    async def genagain_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
        
    @discord.ui.button(label="V1", style=discord.ButtonStyle.primary, custom_id="v1", row=2)
    async def v1_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
        
    @discord.ui.button(label="V2", style=discord.ButtonStyle.primary, custom_id="v2", row=2)
    async def v2_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
    
    @discord.ui.button(label="V3", style=discord.ButtonStyle.primary, custom_id="v3", row=2)
    async def v3_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")
    
    @discord.ui.button(label="V4", style=discord.ButtonStyle.primary, custom_id="v4", row=2)
    async def v4_callback(self, button, interaction):
        await interaction.response.send_message("Generating again...")

优化建议(可选)

如果所有按钮逻辑完全相同,可通过custom_id统一处理,简化代码:

class genagain(discord.ui.View):
    async def button_callback(self, button, interaction):
        button_id = button.custom_id
        await interaction.response.send_message(f"Generating again for {button_id}...")

    def __init__(self):
        super().__init__()
        # 添加U1-U4按钮
        for i in range(1,5):
            self.add_item(discord.ui.Button(label=f"U{i}", style=discord.ButtonStyle.primary, custom_id=f"u{i}", row=0, callback=self.button_callback))
        # 添加刷新按钮
        self.add_item(discord.ui.Button(label="", style=discord.ButtonStyle.primary, emoji="🔄", custom_id="genagain", row=1, callback=self.button_callback))
        # 添加V1-V4按钮
        for i in range(1,5):
            self.add_item(discord.ui.Button(label=f"V{i}", style=discord.ButtonStyle.primary, custom_id=f"v{i}", row=2, callback=self.button_callback))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 04:51:12