Kivy:修改另一个单独类的子窗口小部件

时间:2015-11-06 22:46:51

标签: python python-2.7 kivy

我目前正在考虑从kivy开始进行跨平台开发。我有一点python经验(但基本),现在想在kivy编写一个小游戏进入。我可能不会完成这个,但我喜欢学习东西,同时做一些我感兴趣的东西。 无论如何,我的应用程序"应该分成两个单独的屏幕"顶部仅用于显示内容,所有交互式内容均由底部"屏幕"控制。

现在我希望以旧书的形式显示一些文字,方法是将它写在屏幕上。 这工作正常,但由于某种原因,如果我调用" print_something"那么Label小部件仅在屏幕上更新。从顶部屏幕开始,如果我从底部屏幕调用它,则确实调用了该函数,但Label小部件不会在屏幕上更改。 难道我做错了什么?

以下是代码的剥离版本:

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.clock import Clock

Builder.load_string('''
<MainUI>:
    orientation: 'vertical'
    # both these variables can be the same name and this doesn't lead to
    # an issue with uniqueness as the id is only accessible in kv.
<Screen1>:
    print_txt: print_txt
    layout: layout
    RelativeLayout:
        id: layout
        pos: 0, 400
        size: 480, 400
        Button:
            pos: 0, 200
            size_hint: (1, 0.2)
            text: "Test Print"
            on_press: root.print_something('TEST PRINT FROM SCREEN1')
        AnchorLayout:
            anchor_x: 'center'
            anchor_y: 'bottom'
            Label:
                id: print_txt
                padding_x: 10
                markup: True
                text_size: self.size
                halign: 'left'
                valign: 'top'
                size_hint: (1, 0.2)
                text: ""
<Screen2>:
    btn1: btn1
    RelativeLayout:
        pos: 0, 0
        size: 480, 400
        Button:
            id: btn1
            pos_hint: {'x': .15, 'center_y': .5}
            size_hint: (0.7, 0.5)
            text: "Test Print"
            on_press: root.print_text()
''')


class Screen1(Widget):
    print_txt = ObjectProperty(None)
    layout = ObjectProperty(None)
    def print_something(self, string):
        print 'Function called...'
        self.print_txt.text = ''
        counter = [0]
        string_len = len(string)
        def print_step(dt):
            if counter[0] == string_len:
                return False
            else:
                self.print_txt.text += string[counter[0]]
                counter[0] = counter[0] + 1
        Clock.schedule_interval(print_step, 2.0/60.0)
        print 'Function End..'

class Screen2(Widget):
    btn1 = ObjectProperty(None)

    def __init__(self):
        super(Screen2, self).__init__()

    def print_text(self):
        print 'Trying to print Text from Screen2 to Screen1'
        target = Screen1()
        target.print_something('TEST PRINT FROM SCREEN2')

class MainUI(Widget):
    def __init__(self):
        super(MainUI, self).__init__()
        self.screen1 = Screen1()
        self.add_widget(self.screen1)
        self.add_widget(Screen2())

class MainApp(App):

    def build(self):
        Window.size = (480, 800)
        return MainUI()

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

1 个答案:

答案 0 :(得分:1)

您的Screen2 print_text方法会创建一个 new Screen1实例,该实例已修改但未在任何地方显示,因此您看不到任何更改。

您可以将通话更改为

on_press: root.parent.screen1.print_text()

...访问您实际想要更新的Screen1实例的print_text函数。