Created
September 19, 2019 17:07
-
-
Save GravenilvecTV/ea67f51a84deacb456cf9bc4a0fffff9 to your computer and use it in GitHub Desktop.
CORRIGE TP 2 : GENERATEUR DE CHOIX DE REPAS (UI)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
from tkinter import * | |
def generate_random_meal_suggestion(): | |
if os.path.exists("meals.txt"): | |
with open("meals.txt", "r") as file: | |
meal_random_choice = random.choice(file.readlines()) | |
random_meal_title.config(text=meal_random_choice, font=("Helvetica", 20), bg="#E9EA81", fg="#223E44") | |
else: | |
print("La ressource meals.txt n'existe pas") | |
# creer la fenetre | |
window = Tk() | |
window.title("Qu'allons nous manger ?") | |
window.geometry("720x480") | |
window.config(background='#E9EA81') | |
# creer la frame principale | |
frame = Frame(window, bg="#E9EA81") | |
# creer un titre | |
label_title = Label(frame, text="Qu'allons nous manger ?", font=("Helvetica", 20), bg="#E9EA81", fg="#223E44") | |
label_title.pack() | |
# choix au hasard | |
random_meal_title = Label(frame, text="Soupe", font=("Helvetica", 20), bg="#E9EA81", fg="#223E44") | |
random_meal_title.pack() | |
# creer un bouton | |
generate_password_button = Button(frame, text="Je sais pas", font=("Helvetica", 20), bg="#E9EA81", fg="#223E44", command=generate_random_meal_suggestion) | |
generate_password_button.pack(fill=X) | |
# afficher la frame | |
frame.pack(expand=YES) | |
# afficher la fenetre | |
window.mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kebab | |
kfc | |
tacos | |
poulet frites | |
salade | |
soupe | |
omelette |
Bonjour Graven ... j'ai essayé de randomiser 2 listes ' images + noms ' ... pff le truc de ouf ... Bing AI m'a bien aidé sur ce coup .
Je découvre le module 'Pillow' et la commande 'zip' .
Un grand merci pour cette formation Python ... tes vidéos tournent en boucle ...j'y découvre á chaque foi de nouvelles petites astuces 😉
Code
from tkinter import *
import random
from PIL import ImageTk, Image #module Pillow
# Créer une liste d'images
images = [
"menus/minichoucroute.jpg",
"menus/minifrite.jpg",
"menus/minipate carbonara.jpg",
"menus/minipdt saucisse.jpg",
"menus/minipoisson.jpg",
"menus/minipoisson2.jpg",
"menus/minisalade.jpg"
]
#importer les noms du fichier menus.txt
with open('menus/menus.txt', 'r+') as fichier:
noms = fichier.readlines()
fichier.close()
#creer un objet (variable) avec des paires 'image + nom'
paires = list(zip(images, noms))
#creer une fenetre
fenetre = Tk()
fenetre.title('Menu du jour')
fenetre.iconbitmap('menus/logo3.ico')
fenetre.config(background='white')
#charger l'image avec Pillow
image = ImageTk.PhotoImage(Image.open(paires[0][0]))
#creer un label avec l'image et le nom
label = Label(fenetre, image=image, text=paires[0][1], font=('Balthazar'), bg='white', fg='black', compound='bottom')
#définir une fonction pour changer la paire aléatoire
def changer_paire(event):
#choisir une nouvelle paire
paire_aleatoire = random.choice(paires)
#charger la nouvelle image avec Pillow
image = ImageTk.PhotoImage(Image.open(paire_aleatoire[0]))
#mettre le label á jour avec la nouvelle image et le nouveau nom
label.config(image=image, text=paire_aleatoire[1])
# Garder une référence à l'image pour éviter qu'elle soit effacée par le ramasse-miettes ;)
label.image = image
#associer la fonction au clic sur le label
label.bind("<Button-1>", changer_paire)
#afficher le label
label.pack()
#afficher la fenetre (lance la boucle principale)
fenetre.mainloop()
Résultat
Images réalisées avec Bing AI Creator... Gif animé avec Gimp
Bonjour à tous, voici ma version je vous l'accorde elle est uniquement fonctionnelle haha
# Créer une interface graphique pour le générateur de repas
# Importation des bibliothèques
import os , random
from tkinter import *
# Création du programme de lecture
def meal_generator():
if os.path.exists("meals.txt"):
with open("meals.txt", "r+") as file:
meals_list = file.readlines()
meal_random_choice = random.choice(meals_list)
meal_generate.delete(0, END)
meal_generate.insert(0, meal_random_choice)
file.close()
else:
print("Le document n'existe pas !")
# Création de la fenêtre principale
main_window = Tk()
# Personnalisation de la fenêtre
main_window.title("Générateur de repas")
main_window.geometry("640x360")
main_window.iconbitmap("logoico/meal.ico")
# Création de la boîte principale
main_frame = Frame(main_window)
main_frame.pack(expand=Y)
# Ajout du bouton de génération de repas
generate_meal_button = Button(main_frame, text="Générer un repas", command=meal_generator)
generate_meal_button.pack(fill=X)
# Création du champ de texte
meal_generate = Entry(main_frame)
meal_generate.pack()
# Affichage
main_window.mainloop()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TP1:
import string
from tkinter import *
from random import randint, choice
from PIL import ImageTk, Image
def password_gen():
password_min = 6
password_max = 10
password_option = string.punctuation + string.digits + string.ascii_letters
password = "".join(choice(password_option) for x in range(randint(password_min, password_max)))
window = Tk()
window.geometry("720x480")
window.minsize(480, 360)
window.maxsize(1600, 900)
window.config(bg='#4695AB')
window.iconbitmap("skull.ico")
window.title("password generator")
frame = Frame(window, bg='#4695AB')
frame.pack(expand=YES)
title = Label(frame, font=('arial', 30), text="welcome to password generator ", bg='#4695AB', fg='#F7EF8B')
title.pack()
image = Image.open("image/skull.png")
resiser = image.resize((400, 300), Image.ANTIALIAS)
image = ImageTk.PhotoImage(resiser)
the_image = Label(frame, image=image, bg='#4695AB', height=300, width=400)
the_image.pack()
password_entry = Entry(frame, font=('arial', 25), bg='#F7EF8B', fg='#4695AB', width=25)
password_entry.pack()
#password_counter = Button(window, font=('arial', 20), bg='#4695AB', fg='#F7EF8B', command=password_stock )
#password_counter.pack(side=LEFT)
buttom = Button(frame, text="generate", font=('arial', 15), bg='#F7EF8B', fg='#4695AB', width=10, height=1,command=password_gen)
buttom.pack()
window.mainloop()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TP2:
import random
import os
from tkinter import *
from PIL import ImageTk, Image
def selection_repas():
if os.path.exists("repas.txt"):
window = Tk()
window.geometry("1080x720")
window.title("meals generator")
window.maxsize(1600,900)
window.minsize(480, 360)
window.iconbitmap("skull.ico")
window.config(bg='#F09205')
title = Label(window, font=('arial', 30), text="welcome to the meal menu", bg='#F09205', fg='#FCF6EC')
title.pack(expand=YES)
frame = Frame(window,bg='#F09205')
frame.pack(expand=YES)
image_set = Image.open("food.jpg")
resize = image_set.resize((600, 450), Image.ANTIALIAS)
image_set = ImageTk.PhotoImage(resize)
image = Label(frame, image=image_set, bg='#F09205', height=500, width=600)
image.pack()
#title = Label(window, font=('arial', 30), text="welcome to the meal menu", bg='#F09205', fg='#FCF6EC')
#title.pack()
suggestion =Label(frame, font=('arial', 15), text="to choose the recommendation click here", bg='#F09205', fg='#FCF6EC')
suggestion.pack()
buttom = Button(frame, bg='#A36502', fg='#FCF6EC', height=1, width=20, font=('arial',15), text="click", command=selection_repas)
buttom.pack()
window.mainloop()