ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Kivy in ROS enviroment throws an error: ImportError: No module named app [closed]

asked 2016-04-19 13:31:28 -0500

robopo gravatar image

I would like to have an graphical interface in Kivy for my robot while ROS will works in background.

First will illustrate the project hierarchy.

ros_workscape                  <-catkin_wrokspace
|--src
    |--gui                <---catkin_package
        |--control_panel
            |--__init__.py
            |--graphical_interface.py
        |--srv
            |-- __init__.py
            |--LightsToggle.srv
        |--__init__.py
        |--CMakeLists.txt
        |--package.xml
        |--start_method.py

I made a very simple interface in Kivy:

graphical_interface.py

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.uix.boxlayout import BoxLayout
import rospy
from gui.srv import LightsToggle

class MainMenuScreen(Screen):
    def __init__(self, **kwargs):
        super (MainMenuScreen, self).__init__(**kwargs)

layer = BoxLayout(orientation='vertical')
btn_lights = Button(text="Lights", size_hint_y=None, size_y=100)
btn_lights.bind(on_press=self.lights_toggle)
layer.add_widget(btn_lights)    

    lights = Label(text="Lights: OFF", font_size='18dp', size_hint=(1, .3), halign='left')       
    lights.bind(size=lights.setter('text_size'))      
    layer.add_widget(lights)

self.add_widget(layer)

def lights_toggle(self, *args):
try:
    rospy.wait_for_service('lights_toggle', timeout = 2.0)
    except rospy.ROSException:
        print("Failed. Is server running?")
    return
try:        
    lights_toggle = rospy.ServiceProxy('lights_toggle', LightsToggle)
    response = lights_toggle()
    print ('Lights are :' + response)
except rospy.ServiceException, e:
    print ('Service call failed: ' + e)

class ControlPanelApp(App):     
    def build(self):
        screen_manager = ScreenManager(transition=SlideTransition())
        screen_1 = MainMenuScreen(name='menu')
        screen_manager.add_widget(screen_1)
        return screen_manager


def graphical_interface(cmd_q):
    ControlPanelApp().run()

And this file is run by start_method.py

from control_panel.graphical_interface import graphical_interface
if __name__ == '__main__':
    graphical_interface()

Now the issue:
1) I run catkin_make in ros_workscape - done successfully.
2) source devel/setup.bash
3) rosrun gui start_method.py ( ! roscore is already running)

The code is ok and it works.

It will fails with:

...somepath/ros_workspace/src/gui/control_panel/graphical_interface.py", line 6, in <module> from kivy.app import App ImportError: No module named app

If I do not perform

source devel/setup.bash

then the Kivy works! But it fails on importing the

LightsToggle

If I run the code without Kivy just ROS and with service (with source devel/setup.bash) it works as well.

I guess during the source devel/setup.bash is some system path or Python path overwritten but I can't figure it out.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by robopo
close date 2016-11-02 01:52:35.191902

1 Answer

Sort by » oldest newest most voted
0

answered 2016-04-24 00:55:46 -0500

robopo gravatar image

Finally I found it out. It's not overwritten, it's ok.

It's not a good idea to name your files kivy.py, but even the folders should'nt have the name kivy. I made that mistake and I realized it before writting this question. So I renamed them.

That's why it works without doing the

source devel/setup.bash.

Meanwhile I in my catkin_workspace I ran the catkin_make. And it generated messages to following folder

~/ros_workspace/devel/lib/python2.7/dist-packages

And here was the folder called Kivy and from here it tries to import the Kivy and then it fails.

Be sure you rename everything and delete the rest (especially generated files). :-)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-19 13:31:28 -0500

Seen: 2,614 times

Last updated: Apr 24 '16