You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.8 KiB
148 lines
4.8 KiB
6 months ago
|
import tkinter as tk
|
||
|
from datetime import date
|
||
|
from tkinter import messagebox
|
||
|
|
||
|
|
||
|
class DateSelection:
|
||
|
def __init__(self, master):
|
||
|
"""initialise le widget"""
|
||
|
|
||
|
self.master = master
|
||
|
self.value = "0001-01-01"
|
||
|
|
||
|
frame_btn_date = tk.LabelFrame(self.master, text="Date", font=(25), fg="gray")
|
||
|
frame_btn_date.pack()
|
||
|
|
||
|
self.button = tk.Button(frame_btn_date, text="Choisir une date", command=self.select)
|
||
|
self.button.pack(padx=10, pady=2, side="left")
|
||
|
|
||
|
self.aff_date = tk.Label(frame_btn_date, text=self.value, padx=10, pady=2, font=(25), fg="gray")
|
||
|
self.aff_date.pack(padx=10, pady=2, side="left")
|
||
|
|
||
|
def select(self):
|
||
|
"""permet de choisir une date au format isoformat"""
|
||
|
|
||
|
self.popup = tk.Toplevel(padx=15, pady=15)
|
||
|
self.popup.wm_title("Veuillez choisir une date")
|
||
|
|
||
|
frame_choix = tk.LabelFrame(self.popup, text="jour / mois / année", font=(50), fg="gray")
|
||
|
frame_choix.pack()
|
||
|
|
||
|
self.jour = tk.Spinbox(frame_choix, from_=1, to=31, font=(100), width=2)
|
||
|
self.jour.pack(side="left", padx=10, pady=10)
|
||
|
tk.Label(frame_choix, text="/", font=(100)).pack(side="left")
|
||
|
self.mois = tk.Spinbox(frame_choix, from_=1, to=12, font=(100), width=2)
|
||
|
self.mois.pack(side="left", padx=10, pady=10)
|
||
|
tk.Label(frame_choix, text="/", font=(100)).pack(side="left")
|
||
|
self.annee = tk.Spinbox(frame_choix, from_=1, to=9999, font=(100), width=4)
|
||
|
self.annee.pack(side="left", padx=10, pady=10)
|
||
|
|
||
|
ok_btn = tk.Button(self.popup, text="OK", command=self.update)
|
||
|
ok_btn.pack()
|
||
|
|
||
|
def update(self):
|
||
|
"""assigne la date choisie (si elle est valide) à self.valeur,
|
||
|
sinon, ne change rien"""
|
||
|
|
||
|
val = self.format2iso()
|
||
|
if self.est_valide(val):
|
||
|
self.value = val
|
||
|
self.aff_date['text'] = self.value
|
||
|
self.popup.destroy()
|
||
|
else:
|
||
|
messagebox.showerror("Erreur", "La date n'est pas valide")
|
||
|
|
||
|
def format2iso(self):
|
||
|
"""renvoie une chaine de caractère avec
|
||
|
la date au format iso"""
|
||
|
|
||
|
aaaa = "0"*(4-len(self.annee.get())) + self.annee.get()
|
||
|
mm = "0"*(2-len(self.mois.get())) + self.mois.get()
|
||
|
jj = "0"*(2-len(self.jour.get())) + self.jour.get()
|
||
|
|
||
|
res = aaaa + "-" + mm + "-" + jj
|
||
|
|
||
|
return res
|
||
|
|
||
|
def est_valide(self, d):
|
||
|
"""renvoie True si la date d est valide,
|
||
|
False sinon"""
|
||
|
try:
|
||
|
date.fromisoformat(d)
|
||
|
return True
|
||
|
except ValueError:
|
||
|
return False
|
||
|
|
||
|
def get(self):
|
||
|
"""renvoie la date choisie"""
|
||
|
|
||
|
return self.value
|
||
|
|
||
|
class TemperatureSelection:
|
||
|
def __init__(self, master, nom):
|
||
|
"""initialise le widget"""
|
||
|
|
||
|
self.master = master
|
||
|
|
||
|
self.value = 0
|
||
|
|
||
|
frame_choix = tk.LabelFrame(self.master, text=nom, font=(25), fg="gray")
|
||
|
frame_choix.pack()
|
||
|
|
||
|
self.choix_tmp = tk.Entry(frame_choix, fg="black")
|
||
|
self.choix_tmp.insert("end", self.value)
|
||
|
self.choix_tmp.pack(side="left", padx=(10,5), pady=10)
|
||
|
self.choix_tmp.bind("<KeyRelease>", lambda e : self.isCorrect())
|
||
|
tk.Label(frame_choix, text="°C", font=(50), fg="gray").pack(side="left")
|
||
|
|
||
|
def isCorrect(self):
|
||
|
"""vérifie que la température entrée est correcte"""
|
||
|
val = self.choix_tmp.get()
|
||
|
if is_float(val) and float(val) > -273.15:
|
||
|
self.choix_tmp['bg'] = 'lightgreen'
|
||
|
self.choix_tmp['fg'] = 'black'
|
||
|
return True
|
||
|
else:
|
||
|
self.choix_tmp['bg'] = 'red'
|
||
|
self.choix_tmp['fg'] = 'white'
|
||
|
return False
|
||
|
|
||
|
def get(self):
|
||
|
"""renvoie la température choisie"""
|
||
|
|
||
|
if self.isCorrect():
|
||
|
return float(self.choix_tmp.get())
|
||
|
else:
|
||
|
return 0 #valeur par défaut
|
||
|
|
||
|
|
||
|
class VoisinSelection:
|
||
|
def __init__(self, master):
|
||
|
"""initialise le widget"""
|
||
|
|
||
|
self.master = master
|
||
|
|
||
|
frame_choix = tk.LabelFrame(self.master, text="Nombre de voisins (k)", font=(25), fg="gray")
|
||
|
frame_choix.pack()
|
||
|
|
||
|
self.choix_k = tk.Entry(frame_choix, font=(100), width=3)
|
||
|
self.choix_k.insert("end", "1")
|
||
|
self.choix_k.pack(pady=10)
|
||
|
|
||
|
def get(self):
|
||
|
"""renvoie le nombre de voisins"""
|
||
|
c = self.choix_k.get()
|
||
|
return int(c) if c.isdigit() and int(c) != 0 else 1
|
||
|
|
||
|
|
||
|
#fonction axiliaire
|
||
|
def is_float(element):
|
||
|
if element is None:
|
||
|
return False
|
||
|
try:
|
||
|
float(element)
|
||
|
return True
|
||
|
except ValueError:
|
||
|
return False
|
||
|
|
||
|
|