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

如何用Discord.py实现带交互的4×4按钮矩阵并成行显示点击数字

解决方案

要实现点击按钮后按顺序将数字排列显示在Embed中,核心是在View类里维护一个点击序列,每次点击时更新序列并刷新Embed内容。以下是具体修改方案:

核心修改点

  1. 在Yamila类的初始化方法中添加self.click_sequence属性,用来存储用户点击的数字序列。
  2. 抽离通用的点击处理逻辑,避免每个按钮写重复代码。
  3. 补充4×4矩阵所需的全部16个按钮(原代码只有9个)。

优化后的代码(简洁版,动态生成按钮)

class Yamila(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None
        self.click_sequence = []  # 存储点击的数字序列

        # 循环生成4×4的按钮矩阵
        for num in range(1, 17):
            row = (num - 1) // 4  # 计算按钮所在行,每行4个
            button = discord.ui.Button(
                label=str(num),
                style=discord.ButtonStyle.green,
                row=row
            )
            # 绑定点击事件
            button.callback = self.on_button_click
            self.add_item(button)

    async def on_button_click(self, interaction: discord.Interaction):
        # 获取点击的按钮数字
        clicked_num = interaction.component.label
        self.click_sequence.append(clicked_num)
        # 把序列拼成空格分隔的字符串
        display_text = ' '.join(self.click_sequence)
        # 更新Embed内容
        embed = discord.Embed(description=display_text, color=interaction.user.color or discord.Color.random())
        await interaction.response.edit_message(embed=embed)

保留原方法结构的修改版(适合不想改动太多原代码的情况)

class Yamila(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.value = None
        self.click_sequence = []  # 存储点击的数字序列

    # 通用点击处理函数
    async def handle_click(self, interaction: discord.Interaction, button: discord.ui.Button):
        self.click_sequence.append(button.label)
        display_text = ' '.join(self.click_sequence)
        embed = discord.Embed(description=display_text, color=interaction.user.color or discord.Color.random())
        await interaction.response.edit_message(embed=embed)

    @discord.ui.button(label='1', style=discord.ButtonStyle.green, row=0)
    async def uno(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='2', style=discord.ButtonStyle.green, row=0)
    async def dos(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='3', style=discord.ButtonStyle.green, row=0)
    async def tres(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='4', style=discord.ButtonStyle.green, row=0)
    async def cuatro(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='5', style=discord.ButtonStyle.green, row=1)
    async def cinco(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='6', style=discord.ButtonStyle.green, row=1)
    async def seis(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='7', style=discord.ButtonStyle.green, row=1)
    async def siete(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='8', style=discord.ButtonStyle.green, row=1)
    async def ocho(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='9', style=discord.ButtonStyle.green, row=2)
    async def nueve(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='10', style=discord.ButtonStyle.green, row=2)
    async def diez(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='11', style=discord.ButtonStyle.green, row=2)
    async def once(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='12', style=discord.ButtonStyle.green, row=2)
    async def doce(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='13', style=discord.ButtonStyle.green, row=3)
    async def trece(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='14', style=discord.ButtonStyle.green, row=3)
    async def catorce(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='15', style=discord.ButtonStyle.green, row=3)
    async def quince(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

    @discord.ui.button(label='16', style=discord.ButtonStyle.green, row=3)
    async def dieciseis(self, interaction: discord.Interaction, button: discord.ui.Button):
        await self.handle_click(interaction, button)

两种方案都能实现需求:用户点击按钮后,数字会按点击顺序以空格分隔显示在Embed的描述中。动态生成按钮的版本代码更简洁,适合规则的矩阵布局;保留原方法结构的版本则更贴近你原本的代码写法,改动更小。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 10:45:32