gpt4 book ai didi

python - 带有菜单栏的 Kivy

转载 作者:行者123 更新时间:2023-12-02 00:44:25 25 4
gpt4 key购买 nike

我想创建一个带有始终相同的菜单栏的 Kivy 应用程序。

我的代码:

演示.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

class Display(BoxLayout):
pass

class Screen_One(Screen):
pass

class Screen_Two(Screen):
pass

class DemoApp(App):
def build(self):
return Display()

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

演示.kv:

<Display>:
BoxLayout:
orientation: "vertical"

BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
text: 'One'
on_release: sm.current = 'Screen_One'
Button:
text: 'Two'
on_release: sm.current = 'Screen_Two'

ScreenManager:
id: sm
Screen_One:
Screen_Two:

<Screen_One>:
Button:
text: 'One'

<Screen_Two>:
Button:
text: 'Two'

但它不起作用,如果我点击“一个”,我会收到:

kivy.uix.screenmanager.ScreenManagerException: No Screen with name "Screen_One".

我将我的代码缩减到主要部分。我还尝试将 menu-kivy-code 放在一个单独的文件中并导入,但是我也无法访问 ScreenManager 并且无法切换屏幕。

最佳答案

ScreenManager.current属性是当前显示的屏幕的名称。您尚未在类(屏幕)中设置名称属性。您的 demo.kv 文件应该是:

<Display>:
BoxLayout:
orientation: "vertical"

BoxLayout:
size_hint: 1, None
height: '48dp'
Button:
text: 'One'
on_release: sm.current = 'screen_one' #<<<<<<<<<<<<<<<<
Button:
text: 'Two'
on_release: sm.current = 'screen_two' #<<<<<<<<<<<<<<<<

ScreenManager:
id: sm
Screen_One:
Screen_Two:

<Screen_One>:
name: 'screen_one' #<<<<<<<<<<<<<<<<
Button:
text: 'One'

<Screen_Two>:
name: 'screen_two' #<<<<<<<<<<<<<<<<
Button:
text: 'Two'

enter image description here

关于python - 带有菜单栏的 Kivy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com