CONNEXION
  • RetourJeux
    • Sorties
    • Hit Parade
    • Les + populaires
    • Les + attendus
    • Soluces
    • Tous les Jeux
    • Gaming
  • RetourActu Gaming
    • News
    • Astuces
    • Tests
    • Previews
    • Toute l'actu gaming
  • RetourBons plans
    • Bons plans
    • Bons plans Smartphone
    • Bons plans Hardware
    • Bons plans Image et Son
    • Bons plans Amazon
    • Bons plans Cdiscount
    • Bons plans Decathlon
    • Bons plans Fnac
    • Tous les Bons plans
  • RetourJVTech
    • Actus High-Tech
    • Intelligence Artificielle
    • Smartphones
    • Mobilité urbaine
    • Hardware
    • Image et son
    • Tutoriels
    • Tests produits High-Tech
    • Guides d'achat High-Tech
    • JVTech
  • RetourCulture
    • Actus Culture
    • Culture
  • RetourVidéos
    • A la une
    • Gaming Live
    • Vidéos Tests
    • Vidéos Previews
    • Gameplay
    • Trailers
    • Chroniques
    • Replay Web TV
    • Toutes les vidéos
  • RetourForums
    • Hardware PC
    • PS5
    • Switch 2
    • Xbox Series
    • Switch
    • Pokemon pocket
    • FC 25 Ultimate Team
    • League of Legends
    • Tous les Forums
  • PC
  • PS5
  • Xbox Series
  • Switch 2
  • PS4
  • One
  • Switch
  • iOS
  • Android
  • MMO
  • RPG
  • FPS
En ce moment Genshin Impact Valhalla Breath of the wild Animal Crossing GTA 5 Red dead 2
Liste des sujets

[TKinter] Problème avec une variante des Entry()

kazuoyamashita
kazuoyamashita
Niveau 7
13 août 2019 à 20:57:41

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

https://image.noelshack.com/fichiers/2019/33/2/1565722525-1.png

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

Message édité le 13 août 2019 à 20:58:31 par kazuoyamashita
kazuoyamashita
kazuoyamashita
Niveau 7
13 août 2019 à 21:01:23

J'imagine que ça doit venir d'un set() avec un ensemble vide ?

kazuoyamashita
kazuoyamashita
Niveau 7
13 août 2019 à 21:04:31

C'est bon j'ai réglé mon problème, ça m'a sauté aux yeux en rédigeant le topic :hap:

Sous forums
  • Aide à l'achat Mac
  • Internet
  • Macintosh
  • Création de sites web
  • Création de Jeux
  • Linux
  • Programmation
  • Steam Deck
  • Hardware