logo
IHM avec Guizero (2)

Quelques exemples intéressants pour vos projets.

Exemple n°1 :

Ici on vous montre comment afficher une image dans un widget Picture.

Vous devez disposer de l'image "cross.png" dans le même répertoire que votre programme.

cross.pngcross.png

from guizero import App, Picture

app = App(title="Traffic Manager 1.0")

picture = Picture(app, image="cross.png")

app.display()

Exemple n°2 :

Cet exemple vous montre l'utilisation du layout "grid". Un layout est un gestionnaire d'affichage. Il permet de gérer le positionnement des widgets dans la fenêtre de l'application les uns par rapport aux autres. Le layout "grid" permet de les positionner sur une grille virtuelle.

from guizero import App, PushButton, Text

def do_button1():
    text1.value = "Buffle"

def do_button2():
    text1.value = "Bison"

app = App(title="Traffic Manager 1.0",layout="grid")
button1 = PushButton(app,text="Animal à cornes", command=do_button1,grid=[0,1])
button2 = PushButton(app,text="Animal au poil", command=do_button2,grid=[1,1])
text1 = Text(app, text="???????????",grid=[0,3])

app.display()

Exemple n°3 :

Un exemple pour raliser une animation. Regarder l'utilisation de la méthode repeat() associés au widgets .

La méthode cancel() permet de suspendre l'animation.

from guizero import App, Drawing

def do_animation():
    global x,y,dx,dy
    x+=dx
    y+=dy
    if x>290 or x<10: dx *=-1
    if y>290 or y<10: dy *=-1
    drawing.clear()
    drawing.rectangle(x, y, x+10, y+10, color="blue")

app = App()
x,y=100,50
dx,dy=5,5
drawing = Drawing(app,height=300, width=300)
drawing.bg="grey"
app.repeat(20,do_animation)

app.display()

Exemple n°4:

Un exemple sur la gestion des évènements souris.

from guizero import App, Drawing
  
def do_this(event_data):
    print("mouse pressed "+event_data.x + " : " + event_data.y )

app = App()
drawing = Drawing(app,height=300, width=300)
drawing.bg="grey"
drawing.rectangle(50,50,90,90, color="blue")
app.when_clicked = do_this

app.display()

Exemple n°5 :

Un exemple sur la gestion des évènements clavier.

from guizero import App, TextBox
 
def do_this(event_data):
    print("key pressed : "+ event_data.key)


app = App()
text = Text(app, text="Test clavier")
app.when_key_pressed = do_this
app.display()

Exemple n°6 :

Un exemple plus complets sur la gestion des évènements.

from guizero import App, Text, TextBox, Slider, Waffle, CheckBox, info

def intro():
    info("events", "this is all about events")

# when return is pressed back the test red and then turn it back to black
def key_pressed(e):

    def text_color_black():
        text_box.text_color = "black"

    # return pressed
    if ord(e.key) == 13:
        e.widget.text_color = "red"
        e.widget.after(1000, text_color_black)

def mouse_enters(e):
    e.widget.bg = "lightblue"

def mouse_leaves(e):
    e.widget.bg = "white"

def right_pressed():
    waffle.set_pixel(0,0,"green")

def right_released():
    waffle.set_pixel(0,0,"grey")

app = App()
text = Text(app, text="events")
slider = Slider(app)
text_box = TextBox(app)
check = CheckBox(app, "check")
waffle = Waffle(app)

# when clicked
text.when_clicked = intro

# when key pressed
text_box.when_key_pressed = key_pressed

# highlight widget when move over
check.when_mouse_enters = mouse_enters
check.when_mouse_leaves = mouse_leaves
slider.when_mouse_enters = mouse_enters
slider.when_mouse_leaves = mouse_leaves
text_box.when_mouse_enters = mouse_enters
text_box.when_mouse_leaves = mouse_leaves
waffle.when_mouse_enters = mouse_enters
waffle.when_mouse_leaves = mouse_leaves

# right button pressed and released
waffle.when_right_button_pressed = right_pressed
waffle.when_right_button_released = right_released

app.display()

Exemple n°7 :

Un exemple pour jouer des sons.

from playsound import playsound
from guizero import App, PushButton

def do_play():
    playsound('splash.wav',False)

app = App(title="sound player")
button1 = PushButton(app, command=do_play)
button2 = PushButton(app, command=do_play)
app.display()

Ressources :

  1. Prise en main de guizero.
  2. Utilisation des layout (Lire TOUT ce document).

CC BY SA  ( Christophe Béasse - Mai 2019 )