#how to add a button for each item in a list
1 messages · Page 1 of 1 (latest)
Sure
It was a serious answer I left so I could go looking for an appropriate example
ah ty 😅
But I realized I should ask first whether you're using Views or raw components
im using views
You can create Buttons programmatically aka in a loop
how do i do that tho?
Probably in init, just create a loop and call self.add_item
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
using disnake.ui.Button
wait, actually view has a shorthand methods for creating buttons
Nvm it doesn't 
So yeah you need to import the button class and create the buttons with it
alright will try that ty
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
i still dont know i tried some things but it doesnt work
im still quite new to all this
Show what you got
currently nothing anymore sorry
Well we need something to working with
well i got a button now
class ShopButton(disnake.ui.Button):
def __init__(self):
super().__init__(style=disnake.ButtonStyle.grey, label="test")
Why not just Button(style=disnake.ButtonStyle.grey, label="test")
but where do i have to put that then? might be a really stupid question but it feels like im missing something really stupid
inside the loop
oke i have that and then?
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
I don't think it works that way
The way to achieve your goal is to
- loop over the list
- create a button object
- call an add_item method at the end of iteration
That's python, not imaginary disnake construct 
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
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)
ty that helps