简体   繁体   English

Kivy Python:多个屏幕的按钮和类

[英]Kivy Python: buttons and classes for multiple screens

I have a problem with my Kivy Python Code.我的 Kivy Python 代码有问题。 I have 2 screens: 1st is to navigate to the second screen and on the 2nd screen there is a button to add text to a scrollview...navigating is working but it does not add any text to the scrollview...I think I need some help here!我有 2 个屏幕:第一个是导航到第二个屏幕,在第二个屏幕上有一个按钮可以将文本添加到滚动视图......导航正在工作,但它没有向滚动视图添加任何文本......我想我这里需要一些帮助! AttributeError: 'super' object has no attribute ' getattr ' AttributeError: 'super' 对象没有属性 ' getattr '

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock, mainthread
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.lang import Builder


Builder.load_string("""

<MenuScreen>:
    name: 'mainmenu'
    BoxLayout:
        spacing: 1
        orientation: "vertical"

        Label:
            text: "MAIN MENU"
        Button:
            text: 'Go to Screen 2'
            on_release: 
                root.manager.current = 'screen2'
                root.manager.transition.direction = "left"
        Button:
            text: 'Quit'
            on_release: root.manager.current = app.exit_software()
        

<Screen2>:
    name: 'screen2'
    BoxLayout:
        spacing: 1
        orientation: "vertical"

        ScrollView:
            id: scroll_view
            always_overscroll: False
            BoxLayout:
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                Label:
                    id: label
                    text: "You can add some Text here by pressing the button"
                    size_hint: None, None
                    size: self.texture_size 
        Button:
            text: 'Add text!'
            size_hint_y: 0.1
            on_release: app.add_text()
            

        Button:
            text: 'Back to main menu'
            size_hint_y: 0.1
            on_release: 
                root.manager.current = 'mainmenu'
                root.manager.transition.direction = "right"
            
""")

# Declare both screens
class MenuScreen(Screen):
   pass

class Screen2(Screen):
    pass

class AddTextApp(App):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)

    def build(self):
        # Create the screen manager
        sm = ScreenManager()
        sm.add_widget(MenuScreen(name='mainmenu'))
        sm.add_widget(Screen2(name='screen2'))

        return sm

    def add_text(self):
        self.root.ids.label.text += f"Some new Text\n"
        self.root.ids.scroll_view.scroll_y = 0

    def exit_software(self):
       App.get_running_app().stop()


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

Thank you very much in advance!非常感谢您提前!

The error occurred because self.root.ids gets access to widgets located in the root widget of the main class.发生错误是因为self.root.ids可以访问位于主类的根小部件中的小部件。 To access the secondary screen elements, you need to add it to the main class (in your case, in ScreenManager ) and set its id .要访问辅助屏幕元素,您需要将其添加到主类(在您的情况下,在ScreenManager )并设置其id Also, you have a lot of imported excess, so that it is clearly visible, I advise you to use Pycharm or something like that.另外,你有很多进口过剩,所以它是清晰可见的,我建议你使用Pycharm或类似的东西。

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder

kv = """
<MenuScreen>:
    name: 'mainmenu'
    BoxLayout:
        spacing: 1
        orientation: "vertical"

        Label:
            text: "MAIN MENU"
        Button:
            text: 'Go to Screen 2'
            on_release: 
                root.manager.current = 'screen2'
                root.manager.transition.direction = "left"
        Button:
            text: 'Quit'
            on_release: root.manager.current = app.exit_software()


<Screen2>:
    name: 'screen2'
    BoxLayout:
        spacing: 1
        orientation: "vertical"

        ScrollView:
            id: scroll_view
            always_overscroll: False
            BoxLayout:
                size_hint_y: None
                height: self.minimum_height
                orientation: 'vertical'
                Label:
                    id: label
                    text: "You can add some Text here by pressing the button"
                    size_hint: None, None
                    size: self.texture_size 
        Button:
            text: 'Add text!'
            size_hint_y: 0.1
            on_release: app.add_text()


        Button:
            text: 'Back to main menu'
            size_hint_y: 0.1
            on_release: 
                root.manager.current = 'mainmenu'
                root.manager.transition.direction = "right"
                

ScreenManager:
    MenuScreen:
        id: menu_scr
        
    Screen2:
        id: scr_2
        
"""


class MenuScreen(Screen):
    pass


class Screen2(Screen):
    pass


class AddTextApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def build(self):
        return Builder.load_string(kv)

    def add_text(self):
        self.root.ids.scr_2.ids.label.text += f"Some new Text\n"
        self.root.ids.scr_2.ids.scroll_view.scroll_y = 0

    @staticmethod
    def exit_software():
        App.get_running_app().stop()


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM