我正在尝试创建一个可以在屏幕上执行某些功能的应用程序,但该功能在其他屏幕中起作用。所以,我有一个带有screenmanager的应用程序,这个应用程序有两个屏幕,每个屏幕有两个按钮。一个按钮在其他屏幕上执行某些操作,另一个按钮更改屏幕。但我无法在其他屏幕上执行任何操作。我想创建一个数值,我希望这个值可以通过任何屏幕显示和操作。我的实际代码是: 的.py
class PrimeiroScreen(Screen):
def __init__(self, **kwargs):
self.name = 'um'
super(Screen,self).__init__(**kwargs)
def fc1(self):
value = self.manager.get_screen('dois')
self.ids.lb5.value += 5
#self.ids.lb6.value += 5
我不知道如何在其他屏幕中更改此值,以及如何在这两个标签上显示相同的值
class SegundoScreen(Screen):
def __init__(self, **kwargs):
self.name = 'dois'
super(Screen,self).__init__(**kwargs)
def fc1(self):
self.ids.lb6.value += 5
#self.ids.lb5.value += 5
和.kv
<PrimeiroScreen>:
GridLayout:
cols: 1
size_hint: (.2, .4)
pos_hint:{'x': .35, 'y': .7}
#padding: 30, 0
spacing: 0, 10
MyLabel:
text:"Valor {}".format(self.value)
font_size: '20dp'
text_size: self.width, self.height
id: lb5
Button:
text: "Voltar!"
font_size: '20dp'
text_size: self.width - 20, None
size_hint: (.1,.1)
pos_hint:{'x':.45, 'y':.05}
on_release: root.manager.current = 'dois'
Button:
text: "Somar!"
font_size: '20dp'
text_size: self.width - 20, None
size_hint: (.1,.1)
pos_hint:{'x':.45, 'y':.3}
on_press: root.fc1()
<SegundoScreen>:
GridLayout:
cols: 1
size_hint: (.2, .4)
pos_hint:{'x': .35, 'y': .7}
#padding: 30, 0
spacing: 0, 10
MyLabel:
text:"Valor{}".format(self.value)
font_size: '20dp'
text_size: self.width, self.height
id: lb6
Button:
text: "Voltar!"
font_size: '20dp'
text_size: self.width - 20, None
size_hint: (.1,.1)
pos_hint:{'x':.45, 'y':.05}
on_release: root.manager.current = 'um'
Button:
text: "Somar!"
font_size: '20dp'
text_size: self.width - 20, None
size_hint: (.1,.1)
pos_hint:{'x':.45, 'y':.3}
on_press: root.fc1()
答案 0 :(得分:1)
您可以使用ScreenManager
's get_screen
方法获取特定的Screen
。从那里,您可以通过kv:
id
访问小部件
screen2 = self.manager.get_screen('dois')
screen2.ids.lb6.text = 'New text'