[英]Using ROS message and python to update text input field in kivy GUI
I try to design a GUI to handle stepper motors via ROS, kivy and python.我尝试设计一个 GUI 来通过 ROS、kivy 和 python 处理步进电机。 You can find a minimal version of the GUI below.
您可以在下面找到 GUI 的最小版本。 Actually I want to use a ROS message to update a kivy text input field (read only).
实际上,我想使用 ROS 消息来更新 kivy 文本输入字段(只读)。 In the minimal example, pressing the button should transfer the data of the first input field over a local ROS node to the second input field.
在最简单的例子中,按下按钮应该将第一个输入字段的数据通过本地 ROS 节点传输到第二个输入字段。 Actually it seems, that the Callback within the rospy.Subscriber() doesn't enter the Test class.
实际上,rospy.Subscriber() 中的回调似乎没有进入 Test 类。
Thank You for any suggestions!感谢您的任何建议!
main.py主文件
import kivy
kivy.require('1.7.2')
import rospy
from std_msgs.msg import String
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
class Test(BoxLayout):
text_is = 'text before button press'
def Pub(self):
publish = self.ids.test_text_pub.text
try:
ROSNode.new_text_is.publish(publish)
except rospy.ROSInterruptException: pass
class ROSNode(Widget):
def Callback(publish):
print(publish.data) #check if Callback has been called
test.text_is = publish.data
new_text_is = rospy.Publisher('new_text_is', String, queue_size=10)
rospy.Subscriber('new_text_is', String, Callback)
rospy.init_node('talker', anonymous=True)
class TestApp(App):
def build(self):
return Test()
if __name__ == '__main__':
TestApp().run()
test.kv测试.kv
#:kivy 1.0
<Test>:
BoxLayout:
orientation: 'vertical'
Button:
id: test_button
text: 'publish'
on_press: root.Pub()
TextInput:
id: test_text_pub
text: 'text after button press'
TextInput:
id: test_text_sub
text: root.text_is
If you want your text to automatically update, you need to use Kivy properties . 如果您希望文本自动更新,则需要使用Kivy属性 。
from kivy.properties import StringProperty
class Test(BoxLayout):
text_is = StringProperty('text before button press')
Kivy properties support data binding, so any updates to the property will propagate through to any bound widget. Kivy属性支持数据绑定,因此对该属性的任何更新都将传播到任何绑定的窗口小部件。 Binding happens automatically in kv, so when you do this:
绑定会自动在kv中发生,因此在执行此操作时:
text: root.text_is
..you're telling Kivy that when root.text_is
is updated, update my text
also. ..您是在告诉Kivy,当
root.text_is
更新时,还要更新我的text
。
So I found the solution: 所以我找到了解决方案:
I had to add an on_release event to the button. 我必须向按钮添加on_release事件。
on_release: test_text_sub.text = root.text_is
那么最终的代码是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.