【问题标题】:The Screen Manager is not returning Desired Output in Kivy Python屏幕管理器未在 Kivy Python 中返回所需的输出
【发布时间】:2021-04-26 10:38:26
【问题描述】:

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

我想要的是,第一页应该是登录页面,或者在这里,应该显示“连接页面”然后欢迎,然后最后显示一组从 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()

问题在于:按钮集开始显示。当按下 Cross 关闭 kivy 窗口时,会显示登录页面,然后显示“欢迎”,然后再次显示按钮。 我希望它从第二步开始显示。

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

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

【问题讨论】:

    标签: python user-interface kivy scrollview


    【解决方案1】:

    SomeApp 类的 __init__() 方法启动了一个 App,并运行以下行:

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

    并且 runTouchApp()App 完成之前不会返回。因此,当您运行代码时,它会停止一行:

    self.chat_app = SomeApp()
    

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

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

    与:

        self.add_widget(root)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-13
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-22
      相关资源
      最近更新 更多