Kivy:使用ScreenManager标记上一屏幕中的文本

时间:2016-07-26 15:54:47

标签: python-2.7 kivy

我有一个使用ScreenManager的程序,在按下按钮的屏幕之间切换,我想要实现的是第二个屏幕的标签,以及前一个屏幕的文本输入的文字,我的问题似乎因为那些是不同的类,我对于对象编程是相当新的,因此我的问题可能看起来很愚蠢,我认为问题是该程序不会运行'每次按下按钮时类,而只是将当前小部件更改为其他类的小部件。

这是我的代码:

import kivy
kivy.require('1.8.0')

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager 
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput

class ScreenOne(Screen):

    def __init__ (self,**kwargs):
        super (ScreenOne, self).__init__(**kwargs)

        my_box1 = BoxLayout(orientation='vertical')
        self.my_label1 = TextInput(text="BlaBlaBla on screen 1", font_size='24dp')
        my_button1 = Button(text="Go to screen 2",size_hint_y=None, size_y=100)
        my_button1.bind(on_press=self.changer)
        my_box1.add_widget(self.my_label1)
        my_box1.add_widget(my_button1)
        self.add_widget(my_box1)

    def changer(self,*args):
        self.manager.current = 'screen2'

class ScreenTwo(Screen):

    def __init__(self,**kwargs):
        super (ScreenTwo,self).__init__(**kwargs)

        my_box1 = BoxLayout(orientation='vertical')
        my_label1 = Label(text="BlaBlaBla on screen 2",font_size='24dp')
        my_button1 = Button(text="Go to screen 1",size_hint_y=None, size_y=100)
        my_button1.bind(on_press=self.changer)
        my_box1.add_widget(my_label1)
        my_box1.add_widget(my_button1)
        self.add_widget(my_box1)

    def changer(self,*args):
        self.manager.current = 'screen1'

class TestApp(App):

        def build(self):
            my_screenmanager = ScreenManager()
            screen1 = ScreenOne(name='screen1')
            screen2 = ScreenTwo(name='screen2')
            my_screenmanager.add_widget(screen1)
            my_screenmanager.add_widget(screen2)
            return my_screenmanager

if __name__ == '__main__':
    TestApp().run()

任何帮助apreciated

由于

1 个答案:

答案 0 :(得分:1)

您可以使用管理器get_screen()方法并设置其属性,如下所示:

from kivy.app import App
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.label import  Label



class ScreenOne(Screen):

    def __init__(self,**kwargs):
        super(ScreenOne,self).__init__(**kwargs)
        self.my_box1 = BoxLayout(orientation='vertical')
        self.my_label1 = TextInput(text="BlaBlaBla on scr 1", font_size='24dp')
        self.my_button1 = Button(text="Go to screen 2",size_hint_y=None, size_y=100)
        self.my_button1.bind(on_press=self.changer)
        self.my_box1.add_widget(self.my_label1)
        self.my_box1.add_widget(self.my_button1)
        self.add_widget(self.my_box1)

    def changer(self,*args):
        self.manager.get_screen("screen2").my_label1.text = self.my_label1.text  # here you set screen2's my_label1 to your input
        self.manager.current = 'screen2'




class ScreenTwo(Screen):
    def __init__(self,**kwargs):
        super (ScreenTwo,self).__init__(**kwargs)

        self.my_box1 = BoxLayout(orientation='vertical')
        self.my_label1 = Label(text="BlaBlaBla on screen 2",font_size='24dp')
        self.my_button1 = Button(text="Go to screen 1",size_hint_y=None, size_y=100)
        self.my_button1.bind(on_press=self.changer)
        self.my_box1.add_widget(self.my_label1)
        self.my_box1.add_widget(self.my_button1)
        self.add_widget(self.my_box1)

    def changer(self,*args):
        self.manager.current = 'screen1'



class MyApp(App):
    def __init__(self,**kwargs):
        super(MyApp,self).__init__(**kwargs)
        self.sm = ScreenManager()

        self.sm.test = StringProperty("test")

        self.sm.add_widget(ScreenOne(name = "screen1"))
        self.sm.add_widget(ScreenTwo(name = "screen2"))


    def build(self):
        return self.sm



if __name__ == "__main__":
    MyApp().run()