mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Merge pull request #1959 from davep/extend-center-faq-entry
Extend the centering Q in the FAQ
This commit is contained in:
68
FAQ.md
68
FAQ.md
@@ -70,6 +70,74 @@ if __name__ == "__main__":
|
|||||||
ButtonApp().run()
|
ButtonApp().run()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you use the above on multiple widgets, you'll find they appear to
|
||||||
|
"left-align" in the center of the screen, like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
+-----+
|
||||||
|
| |
|
||||||
|
+-----+
|
||||||
|
|
||||||
|
+---------+
|
||||||
|
| |
|
||||||
|
+---------+
|
||||||
|
|
||||||
|
+---------------+
|
||||||
|
| |
|
||||||
|
+---------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want them more like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
+-----+
|
||||||
|
| |
|
||||||
|
+-----+
|
||||||
|
|
||||||
|
+---------+
|
||||||
|
| |
|
||||||
|
+---------+
|
||||||
|
|
||||||
|
+---------------+
|
||||||
|
| |
|
||||||
|
+---------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
the best approach is to wrap each widget in a container that individually
|
||||||
|
centers it. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from textual.app import App, ComposeResult
|
||||||
|
from textual.containers import Container
|
||||||
|
from textual.widgets import Button
|
||||||
|
|
||||||
|
class Center( Container ):
|
||||||
|
DEFAULT_CSS = """
|
||||||
|
Center {
|
||||||
|
height: auto;
|
||||||
|
width: 100%;
|
||||||
|
align: center middle;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
class ButtonApp(App):
|
||||||
|
|
||||||
|
CSS = """
|
||||||
|
Screen {
|
||||||
|
align: center middle;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
def compose(self) -> ComposeResult:
|
||||||
|
yield Center(Button("PUSH ME!"))
|
||||||
|
yield Center(Button("AND ME!"))
|
||||||
|
yield Center(Button("ALSO PLEASE PUSH ME!"))
|
||||||
|
yield Center(Button("HEY ME ALSO!!"))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ButtonApp().run()
|
||||||
|
```
|
||||||
|
|
||||||
<a name="how-do-i-pass-arguments-to-an-app"></a>
|
<a name="how-do-i-pass-arguments-to-an-app"></a>
|
||||||
## How do I pass arguments to an app?
|
## How do I pass arguments to an app?
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,11 @@
|
|||||||
title: "How do I center a widget in a screen?"
|
title: "How do I center a widget in a screen?"
|
||||||
alt_titles:
|
alt_titles:
|
||||||
- "centre a widget"
|
- "centre a widget"
|
||||||
|
- "centre widgets"
|
||||||
- "center a control"
|
- "center a control"
|
||||||
- "centre a control"
|
- "centre a control"
|
||||||
|
- "center controls"
|
||||||
|
- "centre controls"
|
||||||
---
|
---
|
||||||
|
|
||||||
To center a widget within a container use
|
To center a widget within a container use
|
||||||
@@ -32,3 +35,71 @@ class ButtonApp(App):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ButtonApp().run()
|
ButtonApp().run()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you use the above on multiple widgets, you'll find they appear to
|
||||||
|
"left-align" in the center of the screen, like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
+-----+
|
||||||
|
| |
|
||||||
|
+-----+
|
||||||
|
|
||||||
|
+---------+
|
||||||
|
| |
|
||||||
|
+---------+
|
||||||
|
|
||||||
|
+---------------+
|
||||||
|
| |
|
||||||
|
+---------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
If you want them more like this:
|
||||||
|
|
||||||
|
```
|
||||||
|
+-----+
|
||||||
|
| |
|
||||||
|
+-----+
|
||||||
|
|
||||||
|
+---------+
|
||||||
|
| |
|
||||||
|
+---------+
|
||||||
|
|
||||||
|
+---------------+
|
||||||
|
| |
|
||||||
|
+---------------+
|
||||||
|
```
|
||||||
|
|
||||||
|
the best approach is to wrap each widget in a container that individually
|
||||||
|
centers it. For example:
|
||||||
|
|
||||||
|
```python
|
||||||
|
from textual.app import App, ComposeResult
|
||||||
|
from textual.containers import Container
|
||||||
|
from textual.widgets import Button
|
||||||
|
|
||||||
|
class Center( Container ):
|
||||||
|
DEFAULT_CSS = """
|
||||||
|
Center {
|
||||||
|
height: auto;
|
||||||
|
width: 100%;
|
||||||
|
align: center middle;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
class ButtonApp(App):
|
||||||
|
|
||||||
|
CSS = """
|
||||||
|
Screen {
|
||||||
|
align: center middle;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
def compose(self) -> ComposeResult:
|
||||||
|
yield Center(Button("PUSH ME!"))
|
||||||
|
yield Center(Button("AND ME!"))
|
||||||
|
yield Center(Button("ALSO PLEASE PUSH ME!"))
|
||||||
|
yield Center(Button("HEY ME ALSO!!"))
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ButtonApp().run()
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user