-2

I have an interface in tkinter in which i have placed some labels, an entry and a button. So, as I input a number in the entry some metrics are calculated in background, particularly self.controller.Configurazione.IncentivoServiceProvider in the label self.incentivo_provider. So, as I input a number in the entry and press the button, I'd like to update the text in the said label, but I keep getting 'NoneType' object has no attribute 'config' referred to the very same label. I tried various solutions, I even tried to define the UpdateLabel function in the scope of InputPercentaliEnergiaCondivisa function, but I keep getting always the same error. Can anyone help me please? Here's the code:


    def SetPercentuale(self, valore):
        print("premuto "+ str(valore))
        self.controller.Configurazione.SetPercentualeRitenzione(100-valore)

    def UpdateLabel(self, event): 
        print("Entro qua e percentuale ritenzione vale: "+str(self.controller.Configurazione.PercentualeRitenzione))
        print("Entro qua e incentivo vale: "+str(self.controller.Configurazione.IncentivoServiceProvider))
        self.incentivo_provider.config(text="L'ammontare destinato al service provider risulta: "+str(self.controller.Configurazione.IncentivoServiceProvider))
        self.incentivo_provider.update()


    def InputPercentualiEnergiaCondivisa(self):

        
        self.PercentualeRitenzione = 0
        tk.Label(self, text="Specificare la percentuale attribuibile agli utenti").pack()
        
        self.percentuale_attribuibile = tk.Entry(self, textvariable=tk.IntVar())
        self.percentuale_attribuibile.pack()
        self.button = tk.Button(self, text="Valida") 
        self.button.bind("<Button-1>", lambda event: self.SetPercentuale(int(self.percentuale_attribuibile.get()))) 
        self.button.bind("<Button-1>", self.UpdateLabel, add='+')
        self.button.pack()
        

        self.PercentualeRitenzione = 100 - int(self.percentuale_attribuibile.get())
        
        #Istanzio la classe Configurazione che salva tutti i dati relativi alla configurazione
        self.controller.Configurazione  = Configurazione(len(self.controller.Proprietari_Impianti), len(self.controller.Proprietari_Superficie), len(self.controller.Consumatori_Semplici), len(self.controller.Finanziatori_Proprietari), len(self.controller.Finanziatori_Semplici), self.TotaleImmessa, self.TotalePrelevata, self.EnergiaCondivisa, self.PercentualeRitenzione)
        

        self.incentivo_provider = tk.Label(self, text="L'ammontare destinato al service provider risulta: "+str(self.controller.Configurazione.IncentivoServiceProvider)).pack()
        tk.Label(self, text="L'incentivo da ripartire agli utenti ammonta a: "+str(self.EnergiaCondivisa*(Parametri.IncentivoMise + Parametri.IncentivoArera)*int(self.percentuale_attribuibile.get()))).pack()
        tk.Label(self, text="Specificare per ogni utente la percentuale dell'incentivo da attribuirgli. ").pack()

  • 3
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – Matiiss Jul 19 '21 at 09:58
  • No, I already checked that. If you see my code, self.incentivo_provider (which is the label I'm interested in modifying) is declared and packed separately. I'm using pack() and not grid(), but I don't think that that is the problem – mandiatodos Jul 19 '21 at 10:01
  • no, it is not separated, I already checked, if that weren't the case I wouldn't have flagged this as a duplicate – Matiiss Jul 19 '21 at 10:02
  • 2
    The line `self.incentivo_provider = tk.Label(...).pack()` is the same issue of the linked question. – acw1668 Jul 19 '21 at 10:05
  • Ahahahah, man I'm so stupid, sorry, you are right. I've been working all morning on a deprecated version of the code. In the newer one I have the pack separated. and it obviously works. Thanks! – mandiatodos Jul 19 '21 at 10:07

1 Answers1

0

In this line -

self.incentivo_provider = tk.Label(self, text="L'ammontare destinato al service provider risulta: "+str(self.controller.Configurazione.IncentivoServiceProvider)).pack()

You need to separate the .pack() onto the next line -

self.incentivo_provider = tk.Label(self, text="L'ammontare destinato al service provider risulta: "+str(self.controller.Configurazione.IncentivoServiceProvider))
self.incentivo_provider.pack()
PCM
  • 2,881
  • 2
  • 8
  • 30