Bug report and Enhancement proposal for UIButtonRow class
Summary
This issue report addresses two key points:
- A bug encountered when instantiating the
UIButtonRowclass with acallbackargument. - A request for an enhancement to allow setting button attributes like height and width when calling
add_button.
If needed, I can split these into separate issues based on our discussion.
Bug Report
System Info
Arcade 3.0.0.dev27
vendor: Intel renderer: Mesa Intel(R) HD Graphics 530 (SKL GT2) version: (4, 6) python: 3.11.9 (main, Jun 17 2024, 15:35:00) [GCC 13.2.1 20240210] platform: linux pyglet version: 2.1.dev2 PIL version: 10.2.0
Description
Instantiating the UIButtonRow class with the callback argument raises a TypeError.
Actual Behavior
When using the callback argument, the following error occurs:
TypeError: UIButtonRow.__init__() got an unexpected keyword argument 'callback'
Expected Behavior
Instantiating the UIButtonRow class with a callback argument should assign the callback to self.on_action.
Steps to Reproduce
def callback(event):
pass
button_row = arcade.gui.constructs.UIButtonRow(callback=callback)
Enhancement Request
Description
There is a need to set button attributes like height and width when calling add_button.
Proposed Change
Allow the add_button method to accept additional arguments for button attributes such as height and width.
Example Implementation
I have included a small patch below to illustrate the changes made locally. Note that this is not a definitive proposal but an example of what works for my use case.
3a4
>
6c7
< from typing import Any, Optional
---
> from typing import Any, Callable, Optional
121a123
> height=1,
129a132
> callback: Callable[UIOnClickEvent, None] = None,
131a135
> height=height,
142a147
> self.on_action = callback
149a155
> height=1,
151c157,159
< button = self.button_factory(text=label, style=style, multiline=multiline)
---
> button = self.button_factory(
> text=label, style=style, multiline=multiline, height=height
> )
Did you also try with 3.0.0.dev29?
I've tried with 3.0.0.dev29 now and I get the same results.
Looking into it
Add button now supports **kwargs which will be past to the button factory, which can also be replaced.
Instead of passing callbacks, please use the following
row = UIButtonRow()
row.add_button("Action 1")
@row.event
def on_action(event: UIOnActionEvent):
if event.action == "Action 1":
...
Still add_button return the button, so you can override the on_click method (not recommended).
I am closing this issue because all mentioned issues are addressed. Feel free to open it again.