0

Pues intento modificar el texto de un label llamado "inf" desde un método llamado "get_information" y me sale error, intente con la función "configure" y me sigue saliendo error, como seria la manera correcta de modificarlo?

Con la funcion "configure" me sale este error:

Traceback (most recent call last):
  File "rrr.py", line 110, in <module>
    r = SystemAppleCore()
  File "rrr.py", line 31, in __init__
    self.get_information()
  File "rrr.py", line 40, in get_information
    inf.configure(text = "Database not generated")
NameError: name 'inf' is not defined

Y Cuando intento modificarla normalmente me sale este error:

typeerror: 'nonetype' object does not support item assignment

Codigo | Python3:

class SystemAppleCore():
    def __init__(self):
        self.root = Tk()
        self.root.title("Home Schoolers")
        self.database = {}
        self.tell = 0

        Label(self.root, text = "Home Schoolers", font=("Comic Sans MS" ,15)).grid(row = 0, column = 0, sticky = W, pady = 10, padx = 5)

        # Frame
        frame = LabelFrame(self.root, text = " Menu of options ", font=("Comic Sans MS" ,12))
        frame.grid(row = 1, column = 0, padx = 5)

        # Frame Inf
        self.frameMessage = LabelFrame(self.root, text = " Information ", font=("Comic Sans MS" ,12))
        self.frameMessage.grid(row = 2, column = 0, pady = 8, padx = 5, sticky = W + E)

        inf = Label(self.frameMessage, text = "").grid(row = 0, column = 0, pady = 5, padx = 5)

        # Buttons
        Button(frame, text = "Options of AppleCores", command = self.optionsAppleCores).grid(row = 0, column = 0, sticky = W + E, pady = 3, padx = 5)
        Button(frame, text = "Create AppleCore", command = self.createAppleCore).grid(row = 1, column = 0, sticky = W +E, pady = 3, padx = 5)
        Button(frame, text = "Exit", command = lambda:self.root.destroy()).grid(row = 2, column = 0, sticky = W +E, pady = 3, padx = 5)

        self.get_information()
        self.root.mainloop()

    def get_information(self):  
        try:
            open("AppleCoreDatabase", "rb").close()
            inf["text"] = "Generated database"

        except:
            inf.configure(text = "Database not generated")

    def accessAppleCore(self):
        pass

    def createAppleCore(self):
        window = Toplevel()
        window.resizable(False, False)

        text = StringVar()

        frame = LabelFrame(window, text = " Create AppleCore ", font=("Comic Sans MS" ,12))
        frame.grid(row = 0, column = 0, pady = 10, padx = 10)

        # AppleCore input
        Label(frame, text = "Name-AppleCore:").grid(row = 0, column = 0, pady = 7, padx = 4)
        name = Entry(frame, textvariable = text).grid(row = 0, column = 1, pady = 7, padx = 4)

        # Message
#       message = Label(window, text = "dawda").grid(row  = 1, column = 0, sticky = W + E)

        def create(name_AppleCore):
            try:
                with open("AppleCoreDatabase", "rb") as file:
                    self.database = pickle.load(file)
            except:
                pass

            if name_AppleCore != "":        
                self.database[name_AppleCore] = {

                    "Notes": [],
                    "Assistance": []
                }

                with open("AppleCoreDatabase", "wb") as file:
                    pickle.dump(self.database, file)

                window.destroy()
                self.get_information()

            else:
#               message["text"] = "| Name-AppleCore is required |"
                pass

        # Button
        Button(frame, text = "Add AppleCore", command = lambda:create(text.get())).grid(row = 2, column = 0, columnspan = 2, sticky = W + E)

    def optionsAppleCores(self):
        try:
            with open("AppleCoreDatabase", "rb") as file:
                self.database = pickle.load(file)

            window = Toplevel()
            window.resizable(False, False)

            frame = LabelFrame(window, text = " AppleCores ", font=("Comic Sans MS" ,12))
            frame.grid(row = 1, column = 1, padx = 5)

            for appleCore in self.database.keys():
                self.tell += 1
                Checkbutton(frame, text = "AppleCore: " + appleCore, font=("Comic Sans MS" ,12)).grid(row = self.tell, column = 0, pady = 3, padx = 5, sticky = W)   

        except:
            pass # mensaje aquiii

De antemano muchas gracias por sus respuestas!!

1 respuesta 1

0

Creo que ya encontré el problema

1.- Dejé estas líneas por separado. Cuando configuras la posición del control en la misma línea en que lo defines, éste ya no devuelve un objeto, sino que un None

self.inf = Label(self.frameMessage, text = "")
self.inf.grid(row = 0, column = 0, pady = 5, padx = 5) 

2.- Luego, cambié el inf por self.inf, en la declaración, y luego dentro de la función get_information(self).

Reescribí el código con las correcciones para que sea más fácil copiar, pegar y ejecutar.

from tkinter import Tk, Label, Button, LabelFrame, W, E
class SystemAppleCore():
    def __init__(self):
        self.root = Tk()
        self.root.title("Home Schoolers")
        self.database = {}
        self.tell = 0

        Label(self.root, text = "Home Schoolers", font=("Comic Sans MS" ,15)).grid(row = 0, column = 0, sticky = W, pady = 10, padx = 5)

        # Frame
        frame = LabelFrame(self.root, text = " Menu of options ", font=("Comic Sans MS" ,12))
        frame.grid(row = 1, column = 0, padx = 5)

        # Frame Inf
        self.frameMessage = LabelFrame(self.root, text = " Information ", font=("Comic Sans MS" ,12))
        self.frameMessage.grid(row = 2, column = 0, pady = 8, padx = 5, sticky = W + E)

        self.inf = Label(self.frameMessage, text = "")
        self.inf.grid(row = 0, column = 0, pady = 5, padx = 5)

        # Buttons
        Button(frame, text = "Options of AppleCores", command = self.optionsAppleCores).grid(row = 0, column = 0, sticky = W + E, pady = 3, padx = 5)
        Button(frame, text = "Create AppleCore", command = self.createAppleCore).grid(row = 1, column = 0, sticky = W +E, pady = 3, padx = 5)
        Button(frame, text = "Exit", command = lambda:self.root.destroy()).grid(row = 2, column = 0, sticky = W +E, pady = 3, padx = 5)

        self.get_information()
        self.root.mainloop()

    def get_information(self):
        try:
            open("AppleCoreDatabase", "rb").close()
            self.inf["text"] = "Generated database"

        except:
            self.inf.configure(text = "Database not generated")

    def accessAppleCore(self):
        pass

    def createAppleCore(self):
        window = Toplevel()
        window.resizable(False, False)

        text = StringVar()

        frame = LabelFrame(window, text = " Create AppleCore ", font=("Comic Sans MS" ,12))
        frame.grid(row = 0, column = 0, pady = 10, padx = 10)

        # AppleCore input
        Label(frame, text = "Name-AppleCore:").grid(row = 0, column = 0, pady = 7, padx = 4)
        name = Entry(frame, textvariable = text).grid(row = 0, column = 1, pady = 7, padx = 4)

        # Message
#       message = Label(window, text = "dawda").grid(row  = 1, column = 0, sticky = W + E)

        def create(name_AppleCore):
            try:
                with open("AppleCoreDatabase", "rb") as file:
                    self.database = pickle.load(file)
            except:
                pass

            if name_AppleCore != "":        
                self.database[name_AppleCore] = {

                    "Notes": [],
                    "Assistance": []
                }

                with open("AppleCoreDatabase", "wb") as file:
                    pickle.dump(self.database, file)

                self.get_information()
                #window.destroy()


            else:
#               message["text"] = "| Name-AppleCore is required |"
                pass

        # Button
        Button(frame, text = "Add AppleCore", command = lambda:create(text.get())).grid(row = 2, column = 0, columnspan = 2, sticky = W + E)

    def optionsAppleCores(self):
        try:
            with open("AppleCoreDatabase", "rb") as file:
                self.database = pickle.load(file)

            window = Toplevel()
            window.resizable(False, False)

            frame = LabelFrame(window, text = " AppleCores ", font=("Comic Sans MS" ,12))
            frame.grid(row = 1, column = 1, padx = 5)

            for appleCore in self.database.keys():
                self.tell += 1
                Checkbutton(frame, text = "AppleCore: " + appleCore, font=("Comic Sans MS" ,12)).grid(row = self.tell, column = 0, pady = 3, padx = 5, sticky = W)   

        except:
            pass # mensaje aquiii
SystemAppleCore()
2
  • Ya lo intente y no funciona, hay alguna manera mas, para hacerlo? Commented el 14 ene. 2019 a las 16:43
  • Si! Funciona, Gracias!! Commented el 15 ene. 2019 a las 18:22

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.