简体   繁体   English

使用 ROS 消息和 python 更新 kivy GUI 中的文本输入字段

[英]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.

相关问题 Python Kivy文本输入 - Python kivy text input Python Kivy:在文本输入字段中隐藏虚拟键盘 - Python Kivy: hide virtual keyboard in Text Input Field 从用户获取文本输入并使用 kivy 打印 python - Getting text input from the user and print it python using kivy GUI:如何在python中使用kivy使用GridLayout? - GUI : How to use GridLayout using kivy in python? 在Kivy中强制更新GUI - Force update GUI in kivy python and kivy input text in first windows after click button print ( text ) as label ( update label ) in second widows - python and kivy input text in first windows after click button print ( text ) as label ( update label ) in second widows ElementNotInteractableException:消息:使用 Selenium 和 Python 向输入字段发送文本时键盘错误无法访问元素 - ElementNotInteractableException: Message: Element is not reachable by keyboard error sending text to input field using Selenium and Python Kivy GUI (Python) 在尝试更新屏幕时陷入分段错误 - Kivy GUI (Python) falling into a segmentation fault when trying to update the screen Kivy for Python:验证后重置焦点或将焦点锁定到文本输入字段 - Kivy for Python: Resetting focus after validation or locking focus to a text input field 在ROS之外使用ROS消息类 - Using ROS message classes outside of ROS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM