如何使用 kivy 在屏幕上添加文本?

问题描述 投票:0回答:1

这是代码,我想在主屏幕上添加文本,但在互联网上没有找到解决方案。 请帮助)谢谢)

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label

Window.clearcolor = '#121212'
class MainApp(App):
    def build(self):
        # Add the main and second screens to the manager, this class does nothing else
        sm.add_widget(MainScreen())
        sm.add_widget(SecondScreen())
        sm.add_widget(ThirdScreen())
        return sm  # Return the manager to work with him later

class MainScreen(Screen):
    def __init__(self):
        super().__init__()
        self.name = 'Main'  # setting the screen name value for the screen manager
        # (it's more convenient to call by name rather than by class)
        main_layout = FloatLayout()  # creating an empty layout that's not bound to the screen
        self.add_widget(main_layout)  # adding main_layout on screen

        # Button to get to the 2nd screen
        three_players = Button(text='3 players',
                            bold=True,
                            background_normal='',
                            background_color='#565656',
                            pos_hint={'center_x': .5, 'center_y': .35},
                            size_hint=(.7, .12),
                            color='#000000')
        three_players.bind(on_press=self.to_second_scrn)

        # Button to get to the 3rd screen
        four_players = Button(text='4 players',
                            bold=True,
                            background_normal='',
                            background_color='#565656',
                            pos_hint={'center_x': .5, 'center_y': .2},
                            size_hint=(.7, .12),
                            color='#000000')
        four_players.bind(on_press=self.to_third_scrn)

        main_layout.add_widget(three_players)
        main_layout.add_widget(four_players) # adding button on layout

    def to_second_scrn(self, *args):
        self.manager.current = 'Second'  # selecting the screen by name (in this case by name "Second")

    def to_third_scrn(self, *args):
        self.manager.current = 'Third'  # selecting the screen by name (in this case by name "Thrid")

    def build(self):
        title = Label(text="ABOBA",
                      pos_hint={'center_x': 1, 'center_y': .9},
                      )

        return title

class SecondScreen(Screen):
    def __init__(self):
        super().__init__()
    # on this screen, I do everything the same as on the main screen to be able to switch back and forth
        self.name = 'Second'
        second_layout = FloatLayout()
        self.add_widget(second_layout)

        Button
        Go_Back = Button(text='Go to Main screen',
                         size_hint=(.5, .5),
                         pos_hint={'center_x': .5, 'center_y': .5},
                         color='#ffff00')
        Go_Back.bind(on_press=self.to_main_scrn)

        second_layout.add_widget(Go_Back)

    def to_main_scrn(self, *args):  # together with the click of the button, it transmits info about itself.
        # In order not to pop up an error, add *args to the function
        self.manager.current = 'Main'
        return 0


class ThirdScreen(Screen):
    def __init__(self):
        super().__init__()
    # on this screen, do everything the same as on the main screen to be able to switch back and forth
        self.name = 'Third'
        third_layout = FloatLayout()
        self.add_widget(third_layout)

        # Button
        Go_Back = Button(text='Go to Main screen',
                         size_hint=(.5, .5),
                         pos_hint={'center_x': .5, 'center_y': .5},
                         color='#ffff00')

        Go_Back.bind(on_press=self.to_main_scrn)

        third_layout.add_widget(Go_Back)

    def to_main_scrn(self, *args):  # together with the click of the button, it transmits info about itself.
        # In order not to pop up an error, add *args to the function
        self.manager.current = 'Main'
        return 0


sm = ScreenManager()

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

尝试将新函数添加到返回标签的 MainApp 类中,但没有成功。 但是如果我将它添加到构建函数中,它就会起作用,但其他任何东西都不起作用。 hdddddddddddddddddddddddddd

python python-3.x kivy
1个回答
0
投票

要从 App 类将 Label 添加到主屏幕,您需要访问小部件树并获取已添加到屏幕的 FloatLayout,并将 Label 小部件添加到该布局。

App 方法 on_start() 在构建完成后调用。 将下面的代码添加到您的 App 类中,这会将标签添加到主屏幕。

    def on_start(self):
        # get the main screen, FloatLayout
        main_screen_layout = self.root.get_screen('Main').children[0]
        main_screen_layout.add_widget(Label(text="Title", pos_hint={'center_x': 0.5, 'center_y': .9}))
  • 自己是应用程序
  • self.root 是屏幕管理器
  • self.root.get_screen('Main') 是 MainScreen 实例
  • self.root.get_screen('Main').children[0] 是 MainScreen 实例上的 FloatLayout。
© www.soinside.com 2019 - 2024. All rights reserved.