Rishabh Avvari
Rishabh Avvari

Reputation: 45

How to change label text value in another screen in kivy

So I was making an online tic tac toe game using Kivy/KivyMD and im kinda stuck here trying to edit the value of a label of another screen.

here is the main.py

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from requests import get

ip = get('https://api.ipify.org').text

class OptionScreen(Screen):
    pass

class JoinServer(Screen):
    pass

class CreateServer(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class TicTacToeApp(MDApp):
    def __init__(self, **kwargs):
        self.title = "TicTacToe Online"
        super().__init__(**kwargs)

    def build(self):
        TicTacToeApp.build.kv = Builder.load_file('styles\main.kv')
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Gray"

        return TicTacToeApp.build.kv

    def join(self):
        TicTacToeApp.build.kv.current = 'join'
        

    def create(self):
        TicTacToeApp.build.kv.current = 'create'


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

This is the main.kv file:

#:kivy 2.0.0

WindowManager:
    OptionScreen:
        name: 'option'
    JoinServer:
        name: 'join'
    CreateServer:
        name: 'create'


<OptionScreen>:
    MDCard:
        size_hint: None, None
        size: 700, 500
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        spacing: 25
        padding: 25
        orientation: 'vertical'

        MDLabel:
            text: "Choose an option"
            font_size: "28"
            padding_y: 15
            size_hint_y: None
            halign: 'center'
            pos_hint: {'center_x': 0.5,'center_y':0.5}

        MDRoundFlatButton:
            text:"Join Server"
            font_size: 20
            pos_hint: {'center_x':0.5}
            on_press: app.join()

        MDRoundFlatButton:
            text:"Create Server"
            font_size: 20
            pos_hint: {'center_x':0.5}
            on_press: app.create()

        Widget:
            size_hint_y: None
            height: 90
            
<JoinServer>:
    MDCard:
        size_hint: None, None
        size: 700, 500
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        spacing: 25
        padding: 25
        orientation: 'vertical'

        MDLabel:
            text: "Join Server"
            font_size: "28"
            padding_y: 15
            size_hint_y: None
            halign: 'center'
            pos_hint: {'center_x': 0.5,'center_y':0.5}

        Widget:
            size_hint_y: None
            height: 325       

<CreateServer>:
    MDCard:
        size_hint: None, None
        size: 700, 500
        pos_hint: {"center_x": 0.5, "center_y": 0.5}
        elevation: 10
        spacing: 25
        padding: 25
        orientation: 'vertical'

        MDLabel:
            text: "Create Server"
            font_size: 40
            padding_y: 15
            size_hint_y: None
            halign: 'center'
            pos_hint: {'center_x': 0.5,'center_y':0.5}

        MDLabel:
            text: "Server Address: "
            id: address
            font_size: 18
            size_hint_y: None
            halign: 'center'
            pos_hint: {'center_x': 0.5,'center_y':0.5}

        MDTextField:
            mode: 'round'
            id: passw
            hint_text: "Password"
            size_hint_x: None
            width: 150
            font_size: 18
            pos_hint: {'center_x': 0.5}
            

        MDRoundFlatButton:
            text:"Create Server"
            font_size: 20
            pos_hint: {'center_x': 0.5}
            on_press: app.create_s()

        Widget:
            size_hint_y: None
            height: 20

I want to change the text of the label with the id address in the CreateServer screen to something else as soon as i switch to the CreateServer screen.

I was also wondering how to carry out a code as soon as you switch screens in kivy.

Upvotes: 1

Views: 879

Answers (1)

Shashankh_
Shashankh_

Reputation: 168

To change the label with the id address in the CreateServer screen simply go the CreateServer class and do the following changes.

class CreateServer(Screen):
    def on_enter(self, *args):
        self.manager.get_screen("create").ids.address.text = "your new text value"

and to carry out a code when u switch screens. use the on_enter function which was used in the answer above. the on_enter function is triggered as soon as the screen becomes active. I hope it answered your question.

Upvotes: 1

Related Questions