Kivy: Modifying a child widget of another separate class

眉间皱痕 提交于 2021-02-11 15:21:45

问题


im currently looking into kivy to start with crossplatform development. i have a bit of python experience (but basic) and now wanted to code a little game in kivy to get into. i probably wont finish this but i like learning stuff while doing it with something im intrested in. Anyway my "App" is supposed to be seperated in two seperate "screens" the top one is only used for displaying stuff and the all interactive stuff is controlled from the bottom "screen".

Now i want to display some text in old school way by getting it written letter by letter to the screen. This is working fine but for some reason the Label widget is only updated on screen if i call the "print_something" function from the top screen, if i call it from the bottom screen the function is indeed called but the Label widget wont change on screen. Am i doing something wrong?

Here is a stripped version of the code:

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:


Your Screen2 print_text method creates a new Screen1 instance, which is modified but not displayed anywhere so you don't see anything change.

You could change the call to instead something like

on_press: root.parent.screen1.print_text()

...to access the print_text function of the Screen1 instance that you actually want to update.



来源:https://stackoverflow.com/questions/33576913/kivy-modifying-a-child-widget-of-another-separate-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!