为什么下一个屏幕没有显示在kivy应用程序中?

时间:2019-10-24 16:55:16

标签: python kivy screen

我希望当我按下然后释放按钮时,下面的kivy应用能够切换屏幕,但是什么也没有发生,并且终端上没有错误。当我运行该应用程序时,将显示GirisEkrani屏幕,然后当我按下并释放GirisEkrani中的按钮时,应该显示下一个屏幕(GirisEkrani2)。你如何使它工作?

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

Builder.load_string("""
<ekranApp>
    GirisEkrani:
        id: ge
        Button:
            text: "İleri"
            on_release: ge.manager.current = ge.manager.next()

    GirisEkrani2:
        Button:
            id: ge2
            text: "Ileri 2"
            on_release: ge2.manager.current = ge2.manager.next()

    KontrolEkrani:
        id: ke
        Button:
            text: "Geri"
            on_release: ke.manager.current = ke.manager.previous()
""")
class GirisEkrani(Screen):
    pass

class GirisEkrani2(Screen):
    pass

class KontrolEkrani(Screen):
    pass

class ekranApp(App, ScreenManager):
    def build(self):
        #root = ScreenManager()
        #root.add_widget(GirisEkrani(name = "giris_ekrani"))        
        #root.add_widget(GirisEkrani2(name = "giris_ekrani2"))
        #root.add_widget(KontrolEkrani(name = "kontrol_ekrani"))
        return self

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

尽管人们似乎主张使用.kv文件而不是使用纯Python,但我发现当某些问题不起作用时没有出现错误非常令人沮丧。

1 个答案:

答案 0 :(得分:1)

build()的{​​{1}}方法应返回一个App,它将是您的Widget(但您的{{1}返回Widget本身。我怀疑这会引起问题。但是,由于未设置App的名称,因此代码无法正常工作。这是您的App的正确版本:

Screens

此外,您在kv上的Builder.load_string(""" <ekranApp>: GirisEkrani: id: ge name: "giris_ekrani" Button: text: "İleri" on_release: ge.manager.current = ge.manager.next() GirisEkrani2: id: ge2 name: "giris_ekrani2" Button: text: "Ileri 2" on_release: ge2.manager.current = ge2.manager.next() KontrolEkrani: id: ke name: "kontrol_ekrani" Button: text: "Geri" on_release: ke.manager.current = ke.manager.previous() """) 实际上是在id上设置的。