首页 > 解决方案 > 在 Kivy 中执行 app.run() 时如何处理数据

问题描述

我正在尝试从 Kivy 中的 GUI 获取输入并进行处理。我能够得到我需要的值,但只有在 app.run() 执行完成之后。我希望能够与 app.run() 并行处理数据以获得更流畅的体验。我怎样才能做到这一点?

GUI 允许我选择我想要控制的机器人的哪个关节、哪种类型的旋转和旋转方向。我希望能够获取该数据并移动机器人,然后使用 GUI 选择新的关节、旋转和方向以再次移动机器人,而无需重新启动程序。

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput

class MainGUI(App):
    pressed_color = [0,1,0,1]
    def build(self):
        self.joint_selected = ''
        self.rotation_selected = ''
        self.direction_selected = ''
        
        joints = [['1','2','3','4'],
                  ['5','6','7','8']]
        
        rotations = ['Roll','Pitch','Yaw']

        directions = [['Clockwise','Up'],
                     ['Counterclockwise','Down']]

        self.main_layout = BoxLayout(orientation='vertical', padding=10)
        
        clear_button = Button(text='Clear', pos_hint={'center_x':.5, 'center_y':.5})
        clear_button.bind(on_press=self.clearButtonPressed)
        self.main_layout.add_widget(clear_button)

        for row in joints:
            h_layout = BoxLayout()
            for joint in row:
                button = Button(text=joint, pos_hint={'center_x':.5, 'center_y':.5})
                button.bind(on_press=self.jointButtonPressed)
                h_layout.add_widget(button)
            self.main_layout.add_widget(h_layout)
    
        rot_h_layout = BoxLayout()
        for rotation in rotations:
            rotate_button = Button(text=rotation, pos_hint={'center_x':.5, 'center_y':.5})
            rotate_button.bind(on_press=self.rotationButtonPressed)
            rot_h_layout.add_widget(rotate_button)
        self.main_layout.add_widget(rot_h_layout)

        for row in directions:
            dir_h_layout = BoxLayout()
            for direction in row:
                direction_button = Button(text=direction, pos_hint={'center_x':.5, 'center_y':.5})
                direction_button.bind(on_press=self.directionButtonPressed)
                dir_h_layout.add_widget(direction_button)
            self.main_layout.add_widget(dir_h_layout)

        return self.main_layout


    def clearButtonPressed(self, instance):
        self.joint_selected = ''
        self.rotation_selected = ''
        self.direction_selected = ''

        for layer in self.main_layout.children:
            if isinstance(layer, BoxLayout):
                for widget in layer.children:
                    widget.background_color = [1,1,1,1]
            else:
                layer.background_color = [1,1,1,1]

    def jointButtonPressed(self, instance):
        if self.joint_selected == '':
            instance.background_color = self.pressed_color
            self.joint_selected = instance.text
            # return self.joint_selected
            print(self.joint_selected)

    def rotationButtonPressed(self, instance):
        if self.rotation_selected == '':
            instance.background_color = self.pressed_color
            self.rotation_selected = instance.text
            # return self.rotation_selected
            print(self.rotation_selected)
            

    def directionButtonPressed(self, instance):
        if self.direction_selected == '':
            instance.background_color = self.pressed_color
            self.direction_selected = instance.text
            # return self.direction_selected
            print(self.direction_selected)

app = MainGUI()
app.run()


标签: pythonoopuser-interfacekivy

解决方案


推荐阅读