首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何引用屏幕管理器的屏幕

如何引用屏幕管理器的屏幕
EN

Stack Overflow用户
提问于 2021-06-24 17:16:16
回答 1查看 24关注 0票数 0

我还是个新手,似乎在别处找不到我的案例。我想从app主类访问screen类中的方法:

代码语言:javascript
复制
class MainGUI(Screen):
    d_travelled = StringProperty()
    def update_distance(self,dt):
         self.d_travelled = dt

class SecondaryGUI(Screen):
    pass

class GUIManager(ScreenManager):
    pass

class WindowGUI(App):
    def __init__(self):
        super().__init__()
        self.odrive = myDrive.Motors()
        threading.Thread(target = self.RobotControl).start()

    def build(self):
        self.title = 'MyTitle'
        kv = all_imports.Builder.load_file('kv_styles/robot_3.kv')# which instantiate the GUIs
        return kv
    
    def RobotControl(self):
        while True:
            self.root.update_distance(self.odrive.encoder_cal()) <-----Error

我在WindowGUI中有我的机器人指令,需要与kivy并排运行。首先,我想在我的MainGUI上显示编码器读数。但RobotControl中的self.root指的是GUIManager。当然,这会抛出一个错误。如何在RobotControl中引用MainGUI?是否可以将def update_distance(self,dt)放入GUIManager中,并让MainGUI显示编码器读数?如果是这样的话,是怎么做的?

我的kv文件:

代码语言:javascript
复制
#:kivy 2.0
#:import utils kivy.utils
GUIManager:
    MainGUI:
    SecondaryGUI:

<MainGUI>:
    name: "main"

    BoxLayout:
        orientation: "horizontal"
        size: root.width, root.height #covers entire window
        padding: 15
        GridLayout:
            cols: 1
            spacing: 10
            size_hint_x: 0.4
            # padding_right: 50
            BoxLayout:
                orientation:"horizontal"
                size_hint: 1,.4 

                Label:
                    text: "Drop Distance"
                    size_hint: .75,0.1
                
                Button:
                    id: zero_drop
                    text: "Zero"
                    size_hint: .25,.1
                    on_press:app.odrive.zero_encoder_read()
            BoxLayout:
                orientation: "horizontal"
                TextInput:
                    id: Encoder
                    multiline: False
                    readonly: True
                    text: root.d_travelled <-------I want to display it here
                    size_hint_x: 0.75
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-25 09:43:45

从kivy discord频道到el3phanten的致谢。这是我如何解决我的问题的:

代码语言:javascript
复制
from kivy.clock import mainthread

class MainGUI(Screen):
    # Do something

class SecondaryGUI(Screen):
    pass

class GUIManager(ScreenManager):
    pass

class WindowGUI(App):
    d_travelled = StringProperty()
    def __init__(self):
        super().__init__()
        self.odrive = myDrive.Motors()
        threading.Thread(target = self.RobotControl).start()

    def build(self):
        self.title = 'MyTitle'
        kv = all_imports.Builder.load_file('kv_styles/robot_3.kv')
        return kv
    
    def RobotControl(self):
        while True:
            val = self.odrive.encoder_cal()
            self.set_val(val)

    @mainthread 
    def set_val(self, val) :
    self.d_travelled = val

和kv:

代码语言:javascript
复制
    BoxLayout:
                orientation: "horizontal"
                TextInput:
                    id: Encoder
                    multiline: False
                    readonly: True
                    text: app.d_travelled 
                    size_hint_x: 0.75
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68112923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档