【问题标题】:Using ROS message and python to update text input field in kivy GUI使用 ROS 消息和 python 更新 kivy GUI 中的文本输入字段
【发布时间】:2014-06-25 09:48:22
【问题描述】:

我尝试设计一个 GUI 来通过 ROS、kivy 和 python 来处理步进电机。您可以在下面找到 GUI 的最小版本。实际上,我想使用 ROS 消息来更新 kivy 文本输入字段(只读)。在最简单的例子中,按下按钮应该将第一个输入字段的数据通过本地 ROS 节点传输到第二个输入字段。实际上,rospy.Subscriber() 中的回调似乎没有进入 Test 类。

感谢您的任何建议!

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

#: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

【问题讨论】:

    标签: python user-interface message kivy ros


    【解决方案1】:

    如果你想让你的文字自动更新,你需要使用Kivy properties。

    from kivy.properties import StringProperty
    class Test(BoxLayout):
        text_is = StringProperty('text before button press')
    

    Kivy 属性支持数据绑定,因此对属性的任何更新都会传播到任何绑定的小部件。绑定在 kv 中自动发生,所以当你这样做时:

    text: root.text_is
    

    ..你是在告诉 Kivy,当 root.text_is 更新时,也要更新我的 text。

    【讨论】:

    • 这并没有解决问题。我想这与订阅者的回调有关。
    【解决方案2】:

    所以我找到了解决方案:

    我必须为按钮添加一个 on_release 事件。

    on_release: test_text_sub.text = root.text_is
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多