Commit 60eec3a4 authored by JEAN-YVES SGRO's avatar JEAN-YVES SGRO
Browse files

Update 2023-Spring/2023-02-28-session-04/GUI_01.ipynb

parent 23ae7b67
Loading
Loading
Loading
Loading
+207 −0
Original line number Diff line number Diff line
%% Cell type:code id:d62c088e-b0c4-4967-9c68-1f865181b484 tags:

``` python
!pip install guizero
```

%% Output

    Collecting guizero
      Using cached guizero-1.4.0-py3-none-any.whl (44 kB)
    Installing collected packages: guizero
    Successfully installed guizero-1.4.0

%% Cell type:code id:ea4ef204-4627-4473-805c-0250f2e330fd tags:

``` python
!which python
```

%% Output

    /Users/jsgro/opt/anaconda3/bin/python

%% Cell type:code id:1f862253-e735-4d0a-b44c-7a0444a49dfb tags:

``` python
!python -V
```

%% Output

    Python 3.9.13

%% Cell type:code id:abd9aeaa-3392-463a-8384-5990ceaa0ef8 tags:

``` python
from guizero import App, Text
app = App(title="Hello world")
message = Text(app, text="Welcome to the app")
app.display()
```

%% Cell type:markdown id:335c9165-b0b0-4003-b0cd-c0d56dbfecdc tags:

# Chapter 2
## Wanted poster

Need to update PNG image path to:
`"/Users/jsgro/Documents/PythonClub/createguis/Chapter 2 Wanted Poster/tabitha.png"`

%% Cell type:code id:388945df-789d-4d05-aebf-c61bd38e01cd tags:

``` python
# Check PATH and Image PATH
!pwd
!ls "/Users/jsgro/Documents/PythonClub/createguis/Chapter 2 Wanted Poster/tabitha.png"
```

%% Output

    /Users/jsgro/Documents/PythonClub/Analysis
    /Users/jsgro/Documents/PythonClub/createguis/Chapter 2 Wanted Poster/tabitha.png

%% Cell type:code id:33ae29fa-bff4-473f-a549-49a0f188b8c0 tags:

``` python
from guizero import App, Text, Picture

app = App("Wanted!")
app.bg = "#FBFBD0"

wanted_text = Text(app, "WANTED")
wanted_text.text_size = 50
wanted_text.font = "Times New Roman"

cat = Picture(app, image="/Users/jsgro/Documents/PythonClub/createguis/Chapter 2 Wanted Poster/tabitha.png")

app.display()
```

%% Cell type:markdown id:cbb9f9ec-5d29-4518-b080-f6c0f7134041 tags:

# Chapter 3
## Spy Name chooser

%% Cell type:raw id:f5fca9d1-2318-452f-98c9-4dddcaf7e83f tags:

# spy1.py
# CAUSES ERROR because `choose_name` function not defined yet.
# Imports ---------------
from guizero import App, Text, PushButton

# Functions -------------

# App -------------------
app = App("TOP SECRET")

# Widgets ---------------
title = Text(app, "Push the red button to find out your spy name")
button = PushButton(app, choose_name, text="Tell me!")

# Display ---------------
app.display()

%% Cell type:code id:496ce57c-1323-4d4a-b9da-41efeda54400 tags:

``` python
# spy2.py
# Imports ---------------
from guizero import App, Text, PushButton

# Functions -------------
def choose_name():
    print("Button was pressed")

# App -------------------
app = App("TOP SECRET")

# Widgets ---------------
title = Text(app, "Push the red button to find out your spy name")
button = PushButton(app, choose_name, text="Tell me!")

# Display ---------------
app.display()
```

%% Output

    Button was pressed

%% Cell type:code id:28514126-955f-48b3-a664-c39623c97851 tags:

``` python
# spy3.py
# Imports ---------------
from guizero import App, Text, PushButton
from random import choice

# Functions -------------
def choose_name():
    #print("Button was pressed")
    first_names = ["Barbara", "Woody", "Tiberius", "Smokey", "Jennifer", "Ruby"]
    last_names = ["Spindleshanks", "Mysterioso", "Dungeon", "Catseye", "Darkmeyer", "Flamingobreath"]
    spy_name = choice(first_names) + " " + choice(last_names)
    print(spy_name)

# App -------------------
app = App("TOP SECRET")

# Widgets ---------------
title = Text(app, "Push the red button to find out your spy name")
button = PushButton(app, choose_name, text="Tell me!")
button.bg = "red"
button.text_size = 30

# Display ---------------
app.display()
```

%% Output

    Woody Flamingobreath

%% Cell type:markdown id:a2071b57-6f75-4ad1-8484-d8f4822c1367 tags:

Make the chosen name appear **within** the GUI BOX

%% Cell type:code id:2b437689-02d1-4f6c-b36a-495acaaad3fa tags:

``` python
# 03-spy-name-chooser.py
# Imports ---------------

from guizero import App, Text, PushButton
from random import choice

# Functions -------------

def choose_name():
    #print("Button was pressed")
    first_names = ["Barbara", "Woody", "Tiberius", "Smokey", "Jennifer", "Ruby"]
    last_names = ["Spindleshanks", "Mysterioso", "Dungeon", "Catseye", "Darkmeyer", "Flamingobreath"]
    spy_name = choice(first_names) + " " + choice(last_names)
    #print(spy_name)
    name.value = spy_name

# App -------------------

app = App("TOP SECRET")

# Widgets ---------------

title = Text(app, "Push the red button to find out your spy name")
button = PushButton(app, choose_name, text="Tell me!")
button.bg = "red"
button.text_size = 30
name = Text(app, text="")

# Display ---------------

app.display()
```

%% Cell type:code id:f9c490aa-fc02-4069-81f6-8d37075f5209 tags:

``` python
```