首页 > 解决方案 > tkinter NoneType 对象没有属性“配置”:无法更改标签文本

问题描述

我在 tkinter 中有一个界面,我在其中放置了一些标签、一个条目和一个按钮。因此,当我在条目中输入一个数字时,一些指标是在后台计算的,特别是self.controller.Configurazione.IncentivoServiceProvider在 label 中self.incentivo_provider。因此,当我在条目中输入一个数字并按下按钮时,我想更新所述标签中的文本,但我不断收到“NoneType”对象没有引用相同标签的属性“配置”。我尝试了各种解决方案,我什至尝试在 InputPercentaliEnergiaCondivisa 函数的范围内定义 UpdateLabel 函数,但我总是遇到同样的错误。任何人都可以帮助我吗?这是代码:


    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()

标签: pythonfunctiontkintereventsevent-binding

解决方案


在这一行 -

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

您需要将其分隔.pack()到下一行 -

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

推荐阅读