从不同的屏幕获取值(kivy) [英] get values from a different screen (kivy)
本文介绍了从不同的屏幕获取值(kivy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试实现以下目标:通过按从文件夹 B 获取 txt"按钮从标签中捕获文本:id:屏幕文件夹 B 中的 fb 并复制/添加到标签:屏幕文件夹 A 中的 id:fa.请找到以下代码:提前致谢,
I´m trying to achieve the following: capture the text from from Label: id: fb in screen FolderB and copy/add to Label: id: fa in screen FolderA by pressing the Button "get the txt from Folder B". Please find the codes below: Thanks in advance,
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file('foldera.kv')
Builder.load_file('folderb.kv')
class MainScreen(ScreenManager):
pass
class FolderA(Screen):
pass
class FolderB(Screen):
pass
class FTest(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
FTest().run()
文件 ftest.kv(类构建)
<FolderA@FolderA>
<FolderB@FolderB>
<MainScreen>:
FolderA:
name: 'foldera'
FolderB:
name: 'folderb'
文件夹a.kv (boxlayout)
<FolderA>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Folder A'
Button:
text: 'go to Folder B'
on_press: app.root.current = 'folderb'
Label:
id: fa
text: ''
Button:
text: 'get text from Folder B'
on_press: "this is the button where i'm trying to apply the action"
文件夹b.kv (boxlayout)
<FolderB>:
BoxLayout:
orientation: 'vertical'
Label:
text: 'Folder B'
Button:
text: 'go to Folder B'
on_press: app.root.current = 'foldera'
Label:
id: fb
text: 'TEXT: CAPTURE THIS TEXT'
截图
推荐答案
例如,您可以将共享变量保存在 App
类中.
然后您可以通过编写 app.label_a
在 kv 中访问它试试这个例子:
You could save the shared variable in the App
class, for example.
Then you can access it in kv, by writing app.label_a
Try this example:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
Builder.load_string("""
<Manager>:
Screen:
name: 'first'
BoxLayout:
Label:
text: app.label_a
Button:
text:'go to other'
on_press: app.sm.current = 'other'
Screen:
name: 'other'
BoxLayout:
Label:
id: label_b
text: "Screen 2 label. Press button to change."
Button:
text:'Get label text from screen 1'
on_press: label_b.text = app.label_a
""")
class Manager(ScreenManager):
pass
class MyApp(App):
label_a = StringProperty("Screen 1 Label")
def build(self):
self.sm = Manager()
return self.sm
MyApp().run()
这篇关于从不同的屏幕获取值(kivy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文