kivy - python change label in different layout from code

Viewed 27

I am trying to change the text label with id:text_output via buttons I created in a stack layout. This should happen within the 'def get_text()' But I cannot figure out how I can call the label in the Boxlayout in Player1. I have only access to LetterButtons. Googled for hours and couldn't solve it.

py-file

from kivy.lang import Builder
from kivy.properties import StringProperty, ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import random
from wortliste import wortliste
from kivy.uix.stacklayout import StackLayout
from kivy.core.window import Window
letter_list = ["A", "B", "C", "D", "E","F","G","H",'I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','','','Z','','Enter']

Builder.load_file("page_p1.kv")

rate_wort = random.choice(wortliste)
tipp_liste: list = []
tippsammlung: str = ""

class Player1(BoxLayout):
    stroke_text = StringProperty("")
    output_text = ObjectProperty("Guess")
    status = StringProperty("0")
    text_input_str = StringProperty("word status")
    input_status = StringProperty("")
    lives = 6
    points = 0

class LetterButtons(StackLayout):
    letter = StringProperty("0")
    def __init__(self, max_but=30, **kwargs):
    super().__init__(**kwargs)
        for i in range (0,max_but):
            size_width = Window.width / (max_but/2)
            b = (Button(text=letter_list[i], on_press=self.get_text, disabled=False, size_hint=(0.2, None), border=(16, 16, 16, 16), background_color=(0, 0, 0, 1)))
            self.add_widget(b)

def get_text(self, button):
    #print(button.text)
    if button.text in tipp_liste:
        Player1.output_text = StringProperty("Already guessed that letter!")

kv-file

#:import page_p1 page_p1
<Player1>
    id: player1
    orientation: "vertical"
    BoxLayout:
        id: box
        orientation: "vertical"
        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, None
            height: "40dp"
            Label:
                text: "Lives: " + str(root.lives)
            Label:
                text: "Points: " + str(root.points)
            Button:
                text: "settings"
        Image:
            source: "images/s_" + root.status +".jpg"
            allow_stretch: True
            keep_ratio: True
            #size_hint: 0.8, None
            pos_hint: { "center_y": 0.5 }
        Label:
            id: text_output
            size_hint: 1, 0.3
            font_size: "40dp"
            valign: "center"
            focus: True
            multiline: False
            text: root.output_text
        Label:
            id: text_status
            size_hint: 1, 0.3
            font_size: "40dp"
            valign: "center"
            focus: True
            multiline: False
            text: root.stroke_text

        LetterButtons:
            id: letter_buttons1
            background_normal: ''
            background_color: 1, .3, .4, 1
1 Answers

I'm not an expert, this is actually my first stackoverflow post, but I think you can remove "StringProperty" in the get_text function. You've already assigned output_text to ObjectPropety in the class Player1 and your just updating the value, not creating a new property. The function should be a method within the class in order to call self.

Related