如何在kivy的另一个屏幕中更改标签文本值

发布于 2025-01-12 03:42:42 字数 3939 浏览 1 评论 0原文

所以我正在使用 Kivy/KivyMD 制作在线井字游戏,我有点卡在这里试图编辑另一个屏幕的标签值。

这是 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()

这是 main.kv 文件:

#: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

我想更改 id address 的标签文本当我切换到 CreateServer 屏幕时,CreateServer 屏幕会立即切换到其他内容。

我还想知道如何在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2025-01-19 03:42:43

要在 CreateServer 屏幕中更改带有 id address 的标签,只需转到
CreateServer 类并进行以下更改。

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

并在切换屏幕时执行代码。
使用上面答案中使用的 on_enter 函数。一旦屏幕变为活动状态,就会触发 on_enter 函数。
我希望它回答了你的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文