hello! edit most of it to give executable and understandable code. I have the following code:
from tkinter import *
class Tienda:
def __init__(self,ventana):
self.ventana=ventana
self.ventana.title("Mi Tiendita")
#Marco de ventana
marco=LabelFrame(self.ventana,text="Ingresar Producto")
marco.grid(columns=2,pady=10,padx=10,columnspan=4)
#Nombre
Label(marco,text="Nombre").grid(row=0,column=0)
self.Nombre=Entry(marco)
self.Nombre.grid(row=0,column=1)
self.Nombre.focus()
#marco2
marco2=LabelFrame(self.ventana)
marco2.grid(column=0,columnspan=4)
#BotonGuardar
self.guardar=Button(marco2,text="Guardar Datos",command=self.agregarRegistro)
self.guardar.grid(row=2,column=0)
def agregarRegistro(self):
contenido = self.Nombre.get()
print(contenido)
if __name__=="__main__":
ventana=Tk()
aplicacion=Tienda(ventana)
ventana.mainloop()
Basically what I want to do is get the addRecord function from inside the class. I've tried several things but I can't get it to work since taking it out of the class I lose the use of self. Can someone guide me on how to replace the self to be able to execute the function from outside the class?
If I understood correctly, your question is about how to execute the addRecord method of your saved instance in application.
Assuming you don't know python object-oriented programming, I'll give you a brief explanation of how to access the content of a class
POINT NOMENCLATURE
For this you must resort to the nomenclature of the point, for example:
which has the structure
class.method()
This can be done with any variable or function that belongs to a class a variable that belongs to a class is called ATTRIBUTE a function that belongs to a class is called METHOD then remembering these two definitions I explain how to access your methods for example: yes you want to call a method of a class you use
your_class_name.method_name()
!! Remember to add the parentheses or else the method will not be executed
To access a variable that belongs to a class is the same but with the name of the variable
name_of_your_class.name_of_your_attribute
!! Remember that to access a variable you should NOT use parentheses, otherwise python will interpret the name as that of a function and return an error if it does not exist, or a method. !!
POINT NOMENCLATURE APPLIED TO YOUR CLASS
This nomenclature of the point applied to your class would be for example.
to access the frame attribute
to access the frame2 attribute
to access the save attribute
to access the addRecord method
So you can access every attribute or method contained in your class via the dot nomenclature.
To access attributes that are in the constructor (the constructor is the method that is called init ) you simply use the name of the attribute, but to access attributes or methods that are not in the constructor but are contained in a method of the class, you must call the method first and then the attribute. for instance
to access the content attribute you must do the following
this is because the "content" attribute is only found inside the accessRecord method and not in its constructor.
Finally. How to access the accessRegistration method?
then through
Tienda.accederRegistro()
ACCESS TO accessRegistration OF YOUR INSTANCE
But if you want to access the accessRecord method of an INSTANCE of your class, instead of using the name of your class, you should use the name of the variable that contains your INSTANCE. In your case, the Store instance was saved in the application variable, so if you want to use the method and attributes of your instance contained in application, you must use the dot nomenclature to access starting from the variable that contains your instance. for instance.
to access the accessRecord method of the instance contained in the application is the following
remembering to use the square brackets to specify that it is a method.
After accessing the method, you can immediately use the return of the method or save it in a variable
immediate use here, I print the return of the application instance method
save return
Clarification, accessing the methods and attributes of an instance can only be done after it is created, so these statements that I indicate should go after the if__main__
The full answer is:
IN THIS WAY, YOU CAN ACCESS A METHOD OF AN INSTANCE OR CLASS OUTSIDE THE DEFINITION OF THE SAME