Skip to content

Instantly share code, notes, and snippets.

@GravenilvecTV
Created September 19, 2019 17:07
Show Gist options
  • Save GravenilvecTV/ea67f51a84deacb456cf9bc4a0fffff9 to your computer and use it in GitHub Desktop.
Save GravenilvecTV/ea67f51a84deacb456cf9bc4a0fffff9 to your computer and use it in GitHub Desktop.
CORRIGE TP 2 : GENERATEUR DE CHOIX DE REPAS (UI)
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()
kebab
kfc
tacos
poulet frites
salade
soupe
omelette
@Pcommander
Copy link

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