Read gyroscope data and display in graphical interface attributeError: 'NoneType' object has no attribute 'after'

1

I need to read the data from a gyroscope and display it in a graphical interface, however it shows me this error:

  

attributeError: 'NoneType' object has no attribute 'after'

This is my code:

import smbus
import math
import sys
impor time
import numpy as np
import pylab as pl
from Tkinter import *


# Register
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c


def read_byte(reg):
    return bus.read_byte_data(address, reg)

def read_word(reg):
    h = bus.read_byte_data(address, reg)
    l = bus.read_byte_data(address, reg+1)
    value = (h << 8) + l
    return value

def read_word_2c(reg):
    val = read_word(reg)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

def dist(a,b):
    return math.sqrt((a*a)+(b*b))

def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)

def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)


def position():


        # Aktivieren, um das Modul ansprechen zu koennen
        bus.write_byte_data(address, power_mgmt_1, 0)
        gyroskop_xout = read_word_2c(0x43)
        gyroskop_yout = read_word_2c(0x45)
        gyroskop_zout = read_word_2c(0x47)
        beschleunigung_xout = read_word_2c(0x3b)
        beschleunigung_yout = read_word_2c(0x3d)
        beschleunigung_zout = read_word_2c(0x3f)
        beschleunigung_xout_skaliert = beschleunigung_xout / 16384.0
        beschleunigung_yout_skaliert = beschleunigung_yout / 16384.0
        beschleunigung_zout_skaliert = beschleunigung_zout / 16384.0
        print ("X Rotation: " , get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))
        print ("Y Rotation: " , get_y_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))                         
        dato = (get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))
        valor = dato[0:1]
        lectura.set(valor)
        positionX.after(1000, position)


#Interfaz gráfica
ventana = Tk()
ventana.geometry ("1000x500+0+0")
ventana.title("Proyecto Final")

TpositionX = Label(ventana, text = "Posición en X: ").place(x=100 , y=70)

lectura = StringVar()

positionX = Label (ventana, textvariable = lectura).place(x = 100 , y= 100)

BSalir = Button(ventana, text = "Salir", command = exit).place(x = 100, y=130)

positionX(1000, position)

ventana.mainloop()
    
asked by Fatima 31.05.2017 в 18:57
source

1 answer

1

It is a very common error in Tkinter, your instances positionX in the following line:

positionX = Label (ventana, textvariable = lectura).place(x = 100 , y= 100)

Then you try to apply methods on positionX believing it is a label but this is not true. positionX is None because it is not the return of Tkinter.Label but the method place that returns None . You must separate the instance of the label from the use of its place method so that the name points to the Label object and you can use it later:

positionX = Label (ventana, textvariable = lectura)
positionX.place(x = 100 , y= 100)

Apart from that two more things:

  • Import using from Tkinter import * is a bad practice to be avoided. Use from Tkinter import Label, Button , import Tkinter or import Tkinter as tk .

  • On the other hand, using exit will block the Gui , use the quit() method instead. If you use the IDLE as an IDE, it apparently does not work but it is because the IDLE also uses Tkinter and has its own mainloop on which your app also runs. If you test the code in the terminal it will work as it should.

Your code should be:

import smbus
import math
import sys
import time
import numpy as np
import pylab as pl
import Tkinter as tk

#Debes crear la instancia de bus y definir address (modificalo de acuerdo a tu caso)
bus = smbus.SMBus(1)
address = 0x68 #Valor de address

# Register
power_mgmt_1 = 0x6b
power_mgmt_2 = 0x6c


def read_byte(reg):
    return bus.read_byte_data(address, reg)

def read_word(reg):
    h = bus.read_byte_data(address, reg)
    l = bus.read_byte_data(address, reg+1)
    value = (h << 8) + l
    return value

def read_word_2c(reg):
    val = read_word(reg)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

def dist(a,b):
    return math.sqrt((a*a)+(b*b))

def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)

def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)

def position():
    # Aktivieren, um das Modul ansprechen zu koennen
    bus.write_byte_data(address, power_mgmt_1, 0)
    gyroskop_xout = read_word_2c(0x43)
    gyroskop_yout = read_word_2c(0x45)
    gyroskop_zout = read_word_2c(0x47)
    beschleunigung_xout = read_word_2c(0x3b)
    beschleunigung_yout = read_word_2c(0x3d)
    beschleunigung_zout = read_word_2c(0x3f)
    beschleunigung_xout_skaliert = beschleunigung_xout / 16384.0
    beschleunigung_yout_skaliert = beschleunigung_yout / 16384.0
    beschleunigung_zout_skaliert = beschleunigung_zout / 16384.0
    print ("X Rotation: " , get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))
    print ("Y Rotation: " , get_y_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))                         
    dato = (get_x_rotation(beschleunigung_xout_skaliert, beschleunigung_yout_skaliert, beschleunigung_zout_skaliert))
    lectura.set(dato)
    positionX.after(1000, position)

#Interfaz gráfica
ventana = tk.Tk()
ventana.geometry ("1000x500+0+0")
ventana.title("Proyecto Final")

TpositionX = tk.Label(ventana, text = "Posicisión en X: ")
TpositionX.place(x=100 , y=70)

lectura = tk.StringVar()

positionX = tk.Label (ventana, textvariable = lectura)
positionX.place(x = 100 , y= 100)

BSalir = tk.Button(ventana, text = "Salir", command = ventana.quit)
BSalir.place(x = 100, y=130)

positionX.after(100, position)

ventana.mainloop()
    
answered by 31.05.2017 / 19:45
source