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 à 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
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
Résultat
Images réalisées avec Bing AI Creator... Gif animé avec Gimp