File: recipes.md

package info (click to toggle)
python-guizero 1.1.1%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,676 kB
  • sloc: python: 6,286; makefile: 28; sh: 17
file content (131 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download
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
# 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()
```

## 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()
```