我还是个新手,似乎在别处找不到我的案例。我想从app主类访问screen类中的方法:
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文件:
#: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发布于 2021-06-25 09:43:45
从kivy discord频道到el3phanten的致谢。这是我如何解决我的问题的:
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:
BoxLayout:
orientation: "horizontal"
TextInput:
id: Encoder
multiline: False
readonly: True
text: app.d_travelled
size_hint_x: 0.75https://stackoverflow.com/questions/68112923
复制相似问题