diff --git a/docs/en/tutorial/tutorial-2.md b/docs/en/tutorial/tutorial-2.md index 627ff131..e761ac78 100644 --- a/docs/en/tutorial/tutorial-2.md +++ b/docs/en/tutorial/tutorial-2.md @@ -26,7 +26,7 @@ The more interesting file is `app.py` - this contains the logic that creates our ```python import toga -from toga.style.pack import COLUMN, ROW + class HelloWorld(toga.App): def startup(self): @@ -36,19 +36,19 @@ class HelloWorld(toga.App): self.main_window.content = main_box self.main_window.show() + def main(): return HelloWorld() ``` -Let's go through this line by line: +Let's go through this line by line. + +First, we import the `toga` widget toolkit. ```python import toga -from toga.style.pack import COLUMN, ROW ``` -First, we import the `toga` widget toolkit, as well as some style-related utility constants. Our code doesn't use these yet - but we'll make use of them shortly. - Then, we define a class: ```python @@ -60,8 +60,8 @@ Each Toga application has a single `toga.App` instance, representing the running Next, we define a `startup()` method: ```python -def startup(self): - main_box = toga.Box() + def startup(self): + main_box = toga.Box() ``` The first thing the startup method does is to define a main box. Toga's layout scheme behaves similar to HTML. You build an application by constructing a collection of boxes, each of which contains other boxes, or actual widgets. You then apply styles to these boxes to define how they will consume the available window space. @@ -71,7 +71,7 @@ In this application, we define a single box, but we don't put anything into it. Next, we define a window into which we can put this empty box: ```python -self.main_window = toga.MainWindow(title=self.formal_name) + self.main_window = toga.MainWindow(title=self.formal_name) ``` This creates an instance of a `toga.MainWindow`, which will have a title matching the application's name. A Main Window is a special kind of window in Toga - it's a window that is closely bound to the life cycle of the app. When the Main Window is closed, the application exits. The Main Window is also the window that has the application's menu (if you're on a platform like Windows where menu bars are part of the window). @@ -85,8 +85,8 @@ If you have made an error in your code, the main window of the app may not displ We then add our empty box as the content of the main window, and instruct the application to show our window: ```python -self.main_window.content = main_box -self.main_window.show() + self.main_window.content = main_box + self.main_window.show() ``` Last of all, we define a `main()` function. This is what creates the instance of our application: @@ -104,15 +104,13 @@ That's the simplest possible Toga application. Let's put some of our own content Let's do something more interesting with our `HelloWorld` app. -/// note | Note - -When you make these changes, make sure you keep the imports at the top of the file, and the `main()` at the bottom of the file. You only need to update the `HelloWorld` class. +Modify `app.py` so that it looks like this: -/// +```python +import toga +from toga.style.pack import COLUMN, ROW -Modify your `HelloWorld` class inside `src/helloworld/app.py` so that it looks like this: -```python class HelloWorld(toga.App): def startup(self): main_box = toga.Box(direction=COLUMN) @@ -142,37 +140,30 @@ class HelloWorld(toga.App): def say_hello(self, widget): print(f"Hello, {self.name_input.value}") -``` - -Let's look in detail at what has changed. -We're still creating a main box; however, we are now applying a style: -```python -main_box = toga.Box(direction=COLUMN) +def main(): + return HelloWorld() ``` -Toga's built-in layout system is called "Pack". It behaves a lot like CSS. You define objects in a hierarchy - in HTML, the objects are `