2

I am learning python and decided to create a calculator for uniform line movement with OOP and Tkinter. And because i needed a couple of entry boxes i created a class for it, but i am getting errors when triying to extract the text. Here is the code:

from tkinter import * root = Tk() # Crea una ventana basica. SIEMPRE se debe utilizar root.geometry('350x200') # Setea las dimensiones de la ventana root.title("Simple MRU Calculator.") class labels: # Crea labels de texto def __init__(self, texto, fuente, tamañoFuente, columna, rowa): self = Label(root, text=texto, font=(fuente, tamañoFuente) ).grid(column=columna, row=rowa) class entryBox: # Crea entryboxes def __init__(self, largo, columna, rowa): self = Entry(root, width=largo) self.grid(column=columna, row=rowa) entryBox_Distancia = entryBox(10, 1, 0) entryBox_Velocidad = entryBox(10, 1, 1) entryBox_TiempoF = entryBox(10, 1, 2) entryBox_TiempoI = entryBox(10, 1, 3) def calculando(): entryBoxDT = entryBox_Distancia.get() print(entryBoxDT) theLabel_Uno = labels('Distancia Inicial:', 'Arial', 11, 0, 0) theLabel_Dos = labels('Velocidad', 'Arial', 11, 0, 1) theLabel_Tres = labels('Tiempo Final', 'Arial', 11, 0, 2) theLabel_Cuatro = labels('Tiempo Inicial', 'Arial', 11, 0, 3) boton_Uno = Button(root, text='Calcular', command=calculando) boton_Uno.grid(column=1, row=5) theLabel_Cinco = labels('n', 'Arial', 11, 1, 4) root.mainloop() # Inicia la ventana 

The error i am getting with it is this: AttributeError: 'entryBox' object has no attribute 'get'

How do i get the text from each entryBox?

1 Answer 1

3

Your issue is that you are not inheriting from the entry widget. Simply putting self = whatever does not do anything. To make this work use this:

class entryBox(Entry): # inherit from the entry widget def __init__(self, largo, columna, rowa): super().__init__(width=largo) # call the init method of the entry widget. self.grid(column=columna, row=rowa) 

Now entryBox is an actual instance of the entry widget.

This is a good tutorial dealing with inheritance in python.

And this goes into more depth on why redefining self will not work.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, it works now!!! I studied the info you gave me and could apply it other classes to fix problems that i could have in the future

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.