Kivy in ROS enviroment throws an error: ImportError: No module named app [closed]
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.