#how to add a button for each item in a list

1 messages · Page 1 of 1 (latest)

tiny kayak
#

i want a button to be added for each item in a list with the name of that item would that be possible?

frail sky
#

Sure

tiny kayak
#

your very funny btw...

frail sky
#

It was a serious answer I left so I could go looking for an appropriate example

tiny kayak
#

ah ty 😅

frail sky
#

But I realized I should ask first whether you're using Views or raw components

tiny kayak
#

im using views

frail sky
#

You can create Buttons programmatically aka in a loop

tiny kayak
#

how do i do that tho?

frail sky
#

Probably in init, just create a loop and call self.add_item

tiny kayak
#

alright

#

how would the add_item work

frail sky
#

Hmm?

#

You have your list

#

Loop over it creating a button for each item

#

and at the end of each iteration add the button to the view

tiny kayak
#

ye but how do i create the button

#

sorry if i was a bit unclear

frail sky
#

using disnake.ui.Button

#

wait, actually view has a shorthand methods for creating buttons

#

Nvm it doesn't dead

#

So yeah you need to import the button class and create the buttons with it

tiny kayak
#

alright will try that ty

frail sky
#

I'd give you an example from my codebase but I am using a modified view which simplifies the problem

#

Also, after creating the buttons you'll need to assign a callback to them, otherwise they won't do anything

tiny kayak
#

i still dont know i tried some things but it doesnt work

#

im still quite new to all this

frail sky
#

Show what you got

tiny kayak
#

currently nothing anymore sorry

frail sky
#

Well we need something to working with

tiny kayak
#

well i got a button now

class ShopButton(disnake.ui.Button):
    def __init__(self):
        super().__init__(style=disnake.ButtonStyle.grey, label="test")
frail sky
#

Why not just Button(style=disnake.ButtonStyle.grey, label="test")

tiny kayak
#

but where do i have to put that then? might be a really stupid question but it feels like im missing something really stupid

frail sky
#

inside the loop

tiny kayak
#

oke i have that and then?

twin stump
#

lean what is a loop in python

#

learn some python basic

tiny kayak
#

i know what i loop is

#

and i know python basics i need help with the disnake part of things i am new to disnake not to python

frail sky
#

I don't think it works that way

#

The way to achieve your goal is to

  1. loop over the list
  2. create a button object
  3. call an add_item method at the end of iteration
#

That's python, not imaginary disnake construct blaze

tiny kayak
#

nvm like i said it just feels like i miss something really small and stupid that is why i am asking i know the way it should be done and the logic behind it how ever it doesnt work so that is why i am asking to see where i went wrong

#

but if you cant help me that is fine but then please also dont react on this

frail sky
#

ok, bear with minimal example

import disnake
from disnake.ui import Button, View

class MyView:
    def __init__(self, some_list: list) -> None:
        async def shared_callback(interaction: disnake.MessageInteraction) -> None:
            await inter.send("Button clicked")

        for item in some_list:
            button = Button(label=str(item), style=disnake.ButtonStyle.grey)
            button.callback = shared_callback
            self.add_item(button)
tiny kayak
#

ty that helps