How to use on_radio_button_changed() #1931
-
As far as I understood from the documentation, RadioSet is always displayed vertically. Is there a horizontal option? I'm trying out manually placing two radio buttons horizontally. Got stuck figuring out which button was clicked. Here's a sample program: from textual.app import App
from textual.containers import Horizontal
from textual.widgets import RadioButton, Label
class TestRadio(App):
CSS = '''
#container {
height: auto;
align: center middle;
}
#label {
margin: 2;
width: 100%;
}
'''
def compose(self):
self.r_str = RadioButton('str', value=True, name='str')
self.r_repr = RadioButton('repr', name='repr')
with Horizontal(id='container'):
yield self.r_str
yield self.r_repr
self.output = Label(id='label')
yield self.output
def on_radio_button_changed(self, event):
self.output.update(str(event))
def on_mount(self):
self.dark = False
if __name__ == '__main__':
app = TestRadio()
app.run() Here's a sample screenshot: How do I get the RadioButton For PS: While writing this, I realized I could use |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
You can use |
Beta Was this translation helpful? Give feedback.
-
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Header, Footer, RadioSet
class RadioSetsApp( App[ None ] ):
CSS = """
#horizontal {
layout: horizontal;
}
#horizontal RadioButton {
margin-right: 1;
}
"""
def compose( self ) -> ComposeResult:
yield Header()
yield Vertical(
RadioSet( "One", "Two", "Three", id="default" ),
RadioSet( "One", "Two", "Three", id="horizontal" ),
)
yield Footer()
if __name__ == "__main__":
RadioSetsApp().run() |
Beta Was this translation helpful? Give feedback.
-
Regarding getting the button's name, or any other attribute, the event has an from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, RadioSet, RadioButton, Label
class RadioSetButtonApp( App[ None ] ):
CSS = """
Screen {
align: center middle;
}
RadioSet {
width: 50%;
}
"""
def compose( self ) -> ComposeResult:
yield Header()
with RadioSet():
yield RadioButton( "One", name="One" )
yield RadioButton( "Two", name="Two" )
yield RadioButton( "Three", name="Three" )
yield RadioButton( "Four", name="Four" )
yield RadioButton( "Five", name="Five" )
yield Label( "Press a radio button" )
yield Footer()
def on_radio_set_changed( self, event: RadioSet.Changed ):
self.query_one( Label ).update(
f"Pressed: {event.input.pressed_button.label} named {event.input.pressed_button.name}"
)
if __name__ == "__main__":
RadioSetButtonApp().run() |
Beta Was this translation helpful? Give feedback.
-
Hey Sundeep, As of today, there should be a good example in the
|
Beta Was this translation helpful? Give feedback.
Regarding getting the button's name, or any other attribute, the event has an
input
property which is theRadioSet
that caused the event, andRadioSet
has apressed_button
andpressed_index
property; the former for people who need to get at the button itself, the latter for people who only care about the index of the button pressed. So an example of doing what you want would be: