Valider 80f87d1d rédigé par Thorfin89's avatar Thorfin89
Parcourir les fichiers

Renommage des variables. Nettoyage du code

parent a4afe82e
Chargement en cours
Chargement en cours
Chargement en cours
Chargement en cours
+70 −78
Numéro de ligne d'origine Numéro de ligne de diff Ligne de diff
@@ -3,135 +3,127 @@
# Projet initié le 24/12/2023

from tkinter import *
from tkinter import messagebox
import qrcode

from qrcode.constants import ERROR_CORRECT_M
from PIL import Image

COLOR = "#000000"
VERSION = 3
BOXE_SIZE = 5
BORDER = 1

# ===== FONCTIONS =====

# ===== FONCTIONS =====
def new_file():
    pass


def sel():
    selection = str(value.get())
    label.config(text=selection)
def color_selection():
    selection = str(color.get())
    print(str(selection))
    color_label.config(text=selection)


def create_code():
def create_qrcode():
    if texte.get() != "":
        erreur("")
        global logo
        global qrcode_img
        qr = qrcode.QRCode(
            version=3,
            version=VERSION,
            error_correction=ERROR_CORRECT_M,
            box_size=5,
            border=2
            box_size=BOXE_SIZE,
            border=BORDER
        )
        qr.add_data(texte.get())
        qr.make(fit='True')
        img = qr.make_image(fill_color="blue", back_color="white")

        img = qr.make_image(fill_color=COLOR, back_color="white")
        img.save('qrcode.png')

        logo = PhotoImage(file="qrcode.png")
        logovisu.config(image=logo, bg="white")
        qrcode_img = PhotoImage(file="qrcode.png")
        qrcode_frame.config(image=qrcode_img, bg="white")

        img = Image.open("qrcode.png")
        size = img.size
        logovisu.place(x=200 - size[0] // 2, y=200 - size[1] // 2)
    else:
        erreur("Il n'y a pas de données à coder")


def erreur(message):
    error_label = Label(saisie)
    if message != "":
        error_label.config(bg="lightgray", fg="red", text=message)
        qrcode_frame.place(x=198 - size[0] // 2, y=198 - size[1] // 2)
    else:
        error_label.config(bg="lightgray", fg="lightgray", text="======================================")
    error_label.place(x=390, y=10)
        messagebox.showerror("Erreur", "Il n'y a pas de données à coder")


# création de la fenêtre principale
root = Tk()
root.geometry('800x480+100+100')
root.title("ObjQRCode")
root.config(bg="lightgray")
root.config(bg="#CCCCCC")

ESP = 25

# Création de la barre des menus
menuBar = Menu(root)
# Création de la barre de menus
menu_general = Menu(root)

# Création du menu principal 'Fichier'
menuFichier = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="Fichier", menu=menuFichier)
# Création des sous menus :
menuFichier.add_command(label="Nouveau", command=new_file)
menuFichier.add_command(label="Ouvrir")
menuFichier.add_command(label="Enregistrer")
menuFichier.add_command(label="Enregistrer sous")
menuFichier.add_command(label="Quitter", command=quit)

# Création du menu principal 'Fichier'
menuAide = Menu(menuBar, tearoff=0)
menuBar.add_cascade(label="Aide", menu=menuAide)
# Création des sous menus :
menuAide.add_command(label="Info")
menu_fichier = Menu(menu_general, tearoff=0)
menu_general.add_cascade(label="Fichier", menu=menu_fichier)
# Création des sous menus 'Fichiers'
menu_fichier.add_command(label="Nouveau", command=new_file)
menu_fichier.add_command(label="Ouvrir")
menu_fichier.add_command(label="Enregistrer")
menu_fichier.add_command(label="Enregistrer sous")
menu_fichier.add_command(label="Quitter", command=quit)

# Création du menu principal 'Aide'
menu_aide = Menu(menu_general, tearoff=0)
menu_general.add_cascade(label="Aide", menu=menu_aide)
# Création des sous menus 'Aide'
menu_aide.add_command(label="Info")

# Configuration de la barre des menus
root.config(menu=menuBar)
root.config(menu=menu_general)

# ===== FRAMES =====
conf = Frame(root, bg="lightgray")
conf.place(x=0, y=0, width=380, height=400)
left_frame = Frame(root, bg="#CCCCCC")  # cadre de gauche : configuration
left_frame.place(x=0, y=0, width=380, height=420)

visu = Frame(root, bg="gray")
visu.place(x=380, y=20, width=400, height=400)
right_frame = Frame(root, bg="gray")  # cadre de droite : affichage du qrcode
right_frame.place(x=380, y=20, width=400, height=400)

saisie = Frame(root, bg="lightgray")
saisie.place(x=0, y=401, width=800, height=240)
bottom_frame = Frame(root, bg="#CCCCCC")  # cadre du bas : saisie des données à coder
bottom_frame.place(x=0, y=421, width=800, height=220)

# ===== WIDGETS =====

# boutons radios de choix
value = StringVar()
value.set("OUI")
bouton1 = Radiobutton(conf, text="Oui", variable=value, value="OUI", command=sel)
bouton2 = Radiobutton(conf, text="Non", variable=value, value="NON", command=sel)
bouton3 = Radiobutton(conf, text="Peut-être", variable=value, value="JE NE SAIS PAS", command=sel)
bouton1.config(bg="lightgray")
bouton2.config(bg="lightgray")
bouton3.config(bg="lightgray")
bouton1.place(x=10, y=20 + ESP)
bouton2.place(x=10, y=20 + 2 * ESP)
bouton3.place(x=10, y=20 + 3 * ESP)

# titre de la zone de choix
titre = Label(conf, text="CHOIX :", bg="lightgray")
# Choix de la couleur
titre = Label(left_frame, text="Couleur du QRCode :", bg="#CCCCCC")
titre.place(x=10, y=20)

# affichage du choix
label = Label(conf, text="OUI", width=20, height=2, borderwidth=1, relief="solid")
label.config(bg="lightgray")
label.place(x=10, y=10 + 5 * ESP)

# boutons radios de choix
color = StringVar()
color.set(COLOR)

black_btn = Radiobutton(left_frame, bg="#CCCCCC", text="Noir", variable=color, value="#000000", command=color_selection)
blue_btn = Radiobutton(left_frame, bg="#CCCCCC", text="Bleu", variable=color, value="#0000FF", command=color_selection)
red_btn = Radiobutton(left_frame, bg="#CCCCCC", text="Rouge", variable=color, value="#FF0000", command=color_selection)
black_btn.place(x=10, y=20 + ESP)
blue_btn.place(x=10, y=20 + 2 * ESP)
red_btn.place(x=10, y=20 + 3 * ESP)
# affichage de la couleur choisie
color_label = Label(left_frame, text="#000000", width=20, height=2, borderwidth=1, relief="solid")
color_label.config(bg="#CCCCCC")
color_label.place(x=10, y=10 + 5 * ESP)
# bouton de création du QRCode
create_Btn = Button(conf, text="Créer le Code", bg="lightgreen", command=create_code)
create_Btn = Button(left_frame, text="Créer le Code", bg="#CCCCCC", command=create_qrcode)
create_Btn.place(x=260, y=360, width=100, height=40)

# texte à coder
labelTexte = Label(saisie, text='Données à coder :', bg="lightgray")
labelTexte.place(x=10, y=10)
texte = Entry(saisie, width=122)
texte.place(x=30, y=40)
texte_a_coder = Label(bottom_frame, text='Données à coder :', bg="#CCCCCC")
texte_a_coder.place(x=10, y=0)
texte = Entry(bottom_frame, width=122)
texte.place(x=30, y=28)

# affichage du QRcode
logo = ""
logovisu = Label(visu)
logovisu.config(image=logo)
logovisu.place(x=400, y=400)
qrcode_img = ""
qrcode_frame = Label(right_frame)
qrcode_frame.config(image=qrcode_img)
qrcode_frame.place(x=400, y=400)  # on place l'image par défaut hors champ

root.mainloop()