Salut,
Je suis entrain de coder une petite application pour générer des factures et tenir un livre des recettes ; pour ajouter une nouvelle facture, j'ai une fenêtre avec différents widgets qui permettent d'entrer la date de facturation, etc.
J'utilise des widgets qui héritent des Entry() pour vérifier la taille des inputs. J'ai trouvé ça sur un article. Ca marche bien, mais petit problème, ça prérempli mes widgets avec des " {} " sans trop savoir pourquoi. Auriez-vous une idée ?
Screenshot
Code associé
ajouter_facture_UI.py
from tkinter import *
from tkinter.ttk import *
from validating_entry import *
def init(master):
window = Toplevel(master)
window.geometry("545x175+300+300")
window.title("Ajouter une nouvelle facture")
#########
# id
Label(window, text="ID :").place(x=0, y=0, height=20)
MaxLengthEntry(window, maxlength=4).place(x=25, y=0, height=21, width=48)
#########
# nom
Label(window, text="Nom :").place(x=0, y=40, height=20)
Entry(window).place(x=40, y=40, height=21, width=100)
# adresse
Label(window, text="Voie :").place(x=140, y=40, height=20)
Entry(window).place(x=179, y=40, height=21, width=100)
# code postal
Label(window, text="CP :").place(x=278, y=40, height=20)
MaxLengthEntry(window, maxlength=5).place(x=307, y=40, height=21, width=60)
# ville
Label(window, text="Ville :").place(x=370, y=40, height=20)
Entry(window).place(x=409, y=40, height=21, width=122)
#########
# date
Label(window, text="Date :").place(x=0, y=80, height=20)
MaxLengthEntry(window, maxlength=2).place(x=40, y=80, height=21, width=28)
Label(window, text="/").place(x=67, y=75, height=30)
MaxLengthEntry(window, maxlength=2).place(x=77, y=80, height=21, width=28)
Label(window, text="/").place(x=105, y=75, height=30)
MaxLengthEntry(window, maxlength=4).place(x=115, y=80, height=21, width=52)
# tarif horaire
Label(window, text="€/h :").place(x=220, y=80, height=20)
MaxLengthEntry(window, maxlength=4).place(x=250, y=80, height=21, width=52)
# quantité
Label(window, text="Qté :").place(x=305, y=80, height=20)
MaxLengthEntry(window, maxlength=4).place(x=340, y=80, height=21, width=52)
# nature du règlement
Label(window, text="Règ. :").place(x=395, y=80, height=20)
Entry(window).place(x=430, y=80, height=21, width=100)
# valider
Button(window, text="Valider").place(x=450, y=120, height=21)
window.mainloop()
validating_entry.py
""" Fredrik Lundh, " June 30, 1998 " @ http://effbot.org/zone/tkinter-entry-validate.htm
"""
from tkinter import *
class ValidatingEntry(Entry):
# base class for validating entry widgets
def __init__(self, master, value="", **kw):
# apply(Entry.__init__, (self, master), kw)
Entry.__init__(*(self, master), kw)
self.__value = value
self.__variable = StringVar()
self.__variable.set(value)
self.__variable.trace("w", self.__callback)
self.config(textvariable=self.__variable)
def __callback(self, *dummy):
value = self.__variable.get()
newvalue = self.validate(value)
if newvalue is None:
self.__variable.set(self.__value)
elif newvalue != value:
self.__value = newvalue
self.__variable.set(self.newvalue)
else:
self.__value = value
def validate(self, value):
# override: return value, new value, or None if invalid
return value
class MaxLengthEntry(ValidatingEntry):
def __init__(self, master, value="", maxlength=None, **kw):
self.maxlength = maxlength
# apply(ValidatingEntry.__init__, (self, master), kw)
ValidatingEntry.__init__(*(self, master), kw)
def validate(self, value):
if self.maxlength is None or len(value) <= self.maxlength:
return value
return None # new value too long
Article associé http://effbot.org/zone/tkinter-entry-validate.htm
Merci