Skip to content

Instantly share code, notes, and snippets.

@fmaida
Created May 1, 2024 15:46
Show Gist options
  • Save fmaida/6494980ea4b4d3829de4f2694e6b8639 to your computer and use it in GitHub Desktop.
Save fmaida/6494980ea4b4d3829de4f2694e6b8639 to your computer and use it in GitHub Desktop.
"Greeter" is a very simple app made with Textual. All it does is to "greet" the user in the output field. Enter your name in the "input field" and then push the "Submit" button.
from textual.app import App, ComposeResult
from textual.containers import Horizontal
from textual.widgets import Footer, Header, Static, Button, TextArea
class MyApp(App):
def on_mount(self):
self.title = "Greeter" # This line set the app's title
def compose(self) -> ComposeResult:
yield Header()
yield Static("Please enter your name:") # Adds a label
with Horizontal():
yield TextArea(id="input", text="Will McGugan") # Add a text field for the name
yield Button("Submit", id="submit_name") # Adds a button to submit the name
yield Static("The app response:") # Adds a second label
yield TextArea(id="output", text="...") # Adds a text field for the output
def on_button_pressed(self, event: Button.Pressed) -> None:
if event.button.id == "submit_name":
name = self.query_one('#input')
output = self.query_one("#output")
output.text = f"Hello {name.text} !"
if __name__ == '__main__':
app = MyApp()
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment