屏幕管理器未在 Kivy 中返回所需的输出 Python

The Screen Manager is not returning Desired Output in Kivy Python

Python 输出 shell 中没有显示此类错误。

我想要的是,第一页应该是登录页面,或者像这里一样,应该显示“ConnectingPage”,然后是 Welcome,最后显示一组从 0 到 99 命名的按钮。

这是代码:

import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.core.window import Window


class ConnectingPage(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.cols = 2

        self.add_widget(Label(text = "Usename:"))
        self.username = TextInput(multiline=False)
        self.add_widget(self.username)

        self.add_widget(Label(text = "Password:"))
        self.password = TextInput(multiline=False,password = True)
        self.add_widget(self.password)

        self.joinbutton = Button(text="Join")
        self.joinbutton.bind(on_release = self.click_join_button)
        self.add_widget(Label())  
        self.add_widget(self.joinbutton)
        
    def click_join_button(self, instance):
        username = self.username.text
        password = self.password.text
    #if username == "venu gopal" and password == "venjar":

        MyApp.screen_manager.current = "Info"
        MyApp.screen_manager.current = "Chat"
        
class InfoPage(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.cols = 1
        self.message = Label(text = "welcome",halign="center", valign="middle", font_size=30)
        self.add_widget(self.message)

class SomeApp(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        # Make sure the height is such that there is something to scroll.
        layout.bind(minimum_height=layout.setter('height'))
        for i in range(100):
            btn = Button(text=str(i), size_hint_y=None, height=40)
            layout.add_widget(btn)
        root = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        root.add_widget(layout)
        if __name__ == "__main__":
            runTouchApp(root)

class MyApp(App):
    screen_manager = ScreenManager(transition=FallOutTransition(duration=2))  # this make screen_manager a class vaiable

    def build(self):
        # self.screen_manager = ScreenManager()
        self.connecting_page = ConnectingPage()
        screen = Screen(name='Connect')
        screen.add_widget(self.connecting_page)
        self.screen_manager.add_widget(screen)  # add screen to ScreenManager

        # Info page
        self.info_page = InfoPage()
        screen = Screen(name='Info')
        screen.add_widget(self.info_page)
        self.screen_manager.add_widget(screen)  # add screen to ScreenManager

        # Chat App
        self.chat_app = SomeApp()
        screen = Screen(name='Chat')
        screen.add_widget(self.chat_app)
        self.screen_manager.add_widget(screen)  # add screen to ScreenManager 

        # return ConnectingPage()
        return self.screen_manager


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

问题是:开始时显示了一组按钮。当按下十字键关闭 kivy window 时,会显示登录页面,然后显示“欢迎”,然后再次显示按钮。 我想让它从第二步开始显示。

我认为代码中的“第 61 行”出了问题。当代码为 运行 时,它首先显示按钮等。

请帮我找到上述问题的解决方案。

SomeApp class 的 __init__() 方法启动了 App 运行 行:

    if __name__ == "__main__":
        runTouchApp(root)

并且 runTouchApp() 不会 return 直到 App 完成。因此,当您 运行 您的代码时,它会停止一行:

self.chat_app = SomeApp()

要解决这个问题,只需替换:

    if __name__ == "__main__":
        runTouchApp(root)

与:

    self.add_widget(root)