1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
# Recipes
These are examples of how you can use `guizero` to create user interfaces. Don't be restricted to these ideas, check out [Using guizero](start.md) and the [widgets](app.md).
## Hello World
Create a guizero app and display some text.
```python
from guizero import App, Text
app = App()
text = Text(app, text="hello world")
app.display()
```
## Get some text
Get some data from the user using a `TextBox`.
```python
from guizero import App, TextBox
app = App()
name = TextBox(app, text="Enter your name")
app.display()
```
## Push a button
Use a `PushButton` to display a message when the button is pressed.
```python
from guizero import App, TextBox, PushButton, Text
def update_text():
label.value = name.value
app = App()
label = Text(app, text="What's your name?")
name = TextBox(app)
button = PushButton(app, command=update_text)
app.display()
```
## Display an image
Use a `Picture` object to display an image.
```python
from guizero import App, Picture
app = App()
pic = Picture(app, image="myimage.gif")
app.display()
```
## Button with an image
Create a picture `PushButton` with an image like this:
```python
from guizero import App, PushButton
def do_nothing():
print("A picture button was pressed")
app = App()
button = PushButton(app, image="button.gif", command=do_nothing)
app.display()
```
> The image `button.gif` should be stored in the folder as your program. Alternatively you can provide the path to your image.
## Toggle 2 buttons
Have 2 buttons, **start** and **stop** with them changing the `enabled` state of each other.
```python
from guizero import App, PushButton
def start():
start_button.disable()
stop_button.enable()
def stop():
start_button.enable()
stop_button.disable()
app = App()
start_button = PushButton(app, command=start, text="start")
stop_button = PushButton(app, command=stop, text="stop", enabled=False)
app.display()
```
## Change your apps appearance
Your app doesn't have to use the standard colors and text, let your user pick the background and text color from 2 combo's.
```python
from guizero import App, Combo, Text
def update_bg():
app.bg = bg_combo.value
def update_text():
app.text_color = text_combo.value
colors = ["black", "white", "red", "green", "blue"]
app = App()
app.bg = "black"
app.text_color = "white"
title1 = Text(app, text="Background color")
bg_combo = Combo(app, options=colors, selected=app.bg, command=update_bg)
title2 = Text(app, text="Text color")
text_combo = Combo(app, options=colors, selected=app.text_color, command=update_text)
app.display()
```
## Scale an image
Display an image on the screen with 2 sliders, 1 for height and 1 for width.
```python
from guizero import App, Slider, Picture
def resize():
picture.width = width.value
picture.height = height.value
app = App(layout="grid")
picture = Picture(app, image="image.gif", grid=[0,1])
width = Slider(app, command=resize, grid=[0,0], start=1, end=picture.width)
width.width = picture.width
width.value = picture.width
height = Slider(app, command=resize, horizontal=False, grid=[1,1], start=1, end=picture.height)
height.height = picture.height
height.value = picture.height
app.display()
```
## Double click a widget
To be able to react when a user double click's you will need to use [events](events.md).
```python
from guizero import App, Text
def double_click():
double_click_me.value = "Thanks"
app = App()
double_click_me = Text(app, text="Double click me")
double_click_me.when_double_clicked = double_click
app.display()
```
## Update your app using a timer
You can use `repeat` to periodically update your application. For example, a timer which updates every 1 second (1000 ms).
```python
from guizero import App, Text
# Update the counter
def counter():
text.value = int(text.value) + 1
app = App("Hello world")
text = Text(app, text="1")
# Schedule call to counter() every 1000ms
text.repeat(1000, counter)
app.display()
```
## Ask before closing the app
You can use a `yesno` box to check whether someone really wants to exit your app. If they click yes, the app is closed, if not, nothing happens and they can continue with what they were doing.
```python
from guizero import App, Text
# Ask the user if they really want to close the window
def do_this_when_closed():
if app.yesno("Close", "Do you want to quit?"):
app.destroy()
app = App()
title = Text(app, text="blank app")
# When the user tries to close the window, run the function do_this_when_closed()
app.when_closed = do_this_when_closed
app.display()
```
|