繁体   English   中英

为什么我收到 Kivy KeyError: with my code?

[英]Why am I getting a Kivy KeyError: with my code?

我正在尝试创建一个简单的登录名并在下一个屏幕上显示当前时间,但我只是不明白为什么我不断收到此错误。

*.py 文件

from kivy.app import App
from datetime import datetime
from datetime import timedelta
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen


class test(BoxLayout):
    pass

class blank_page(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

kv = Builder.load_file("delete.kv")
class MyApp(App):
    def build(self):

        self.now = datetime.now()
        Clock.schedule_interval(self.update_clock, 1)
        self.my_label = Label(text= self.now.strftime('%H:%M:%S'))
        return kv

    def update_clock(self, *args):
        self.now = self.now + timedelta(seconds = 1)
        self.root.ids['my_label'].text = self.now.strftime('%H:%M:%S')
        print(self.now.strftime('%H:%M:%S'))

MyApp().run()

*.kv 文件


#:kivy 1.0
#:import hex kivy.utils.get_color_from_hex
ScreenManagement:
    test:
    blank_page:
<test>:
    BoxLayout:
        orintation: 'vertical'
        Label:
            text: 'test_label'
<blank_page>:
    BoxLayout:
        orientation:'vertical'
        Label:
            id: my_label
        Button:
            text:'next'
            on_release: app.root.current = "blank_page"

我想要发生的是能够登录,单击按钮将我带到第二个屏幕,它将显示时间。 但我不断收到以下错误:

File "kivy\_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
   File "kivy\_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
   File "kivy\_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
   File "kivy\_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
   File "c:/Users/QQQ/Documents/University work/test.py", line 31, in update_clock
     self.root.ids['my_label'].text = self.now.strftime('%H:%M:%S')
 KeyError: 'my_label'

正如文档指出的那样:

引用小部件

在小部件树中,通常需要访问/引用其他小部件。 Kv 语言提供了一种使用 id 来做到这一点的方法。 将它们视为只能在 Kv 语言中使用的 class 级别变量。 考虑以下:

 <MyFirstWidget>: Button: id: f_but TextInput: text: f_but.state <MySecondWidget>: Button: id: s_but TextInput: text: s_but.state

一个 id 在 scope 中被限制在它被声明的规则中,所以在上面的代码中 s_but 不能在 <MySecondWidget> 规则之外访问。

...

强调我的

换句话说,id my_label 只能通过 blank_page 访问。 在这种情况下,当 root 是 ScreenManager 时,我们将名称设置为 blank_page 以便可以访问它。

您还有以下错误:

  • ScreenManager 的子项必须是 Screen,在您的案例中测试是 BoxLayout,因此您必须将其放置在另一个 Screen 中或将其删除。

  • kv 中使用的类名必须大写。

  • 在 build 方法中创建的 self.my_label 与在.kv 中创建的不同,因此没有必要。

考虑到上述情况,解决方案是:

from kivy.app import App
from datetime import datetime
from datetime import timedelta
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen


class Test(BoxLayout):
    pass


class Blank_Page(Screen):
    pass


class ScreenManagement(ScreenManager):
    pass


kv = Builder.load_file("delete.kv")


class MyApp(App):
    def build(self):
        self.now = datetime.now()
        Clock.schedule_interval(self.update_clock, 1)
        return kv

    def update_clock(self, *args):
        self.now = self.now + timedelta(seconds=1)
        self.root.get_screen("blank_page").ids["my_label"].text = self.now.strftime(
            "%H:%M:%S"
        )
        print(self.now.strftime("%H:%M:%S"))


if __name__ == "__main__":
    MyApp().run()
#:kivy 1.0
#:import hex kivy.utils.get_color_from_hex


ScreenManagement:
    Blank_Page:
        name: "blank_page"
    Screen:
        name: "next_page"
        Test:

<Test>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'test_label'

<blank_page>:
    BoxLayout:
        orientation:'vertical'
        Label:
            id: my_label
        Button:
            text:'next'
            on_release: app.root.current = "next_page"

这是因为my_label不是你的 root 的 id,而是blank_page
您可以通过引用blank_page来解决此问题。
我会做这样的事情。
blank_page设为根的 object 属性。

千伏:

ScreenManagement:
    bp: bp
    test:
    blank_page:
        id: bp
<test>:
    BoxLayout:
        orintation: 'vertical'
        Label:
            text: 'test_label'
<blank_page>:
    BoxLayout:
        orientation:'vertical'
        Label:
            id: my_label
        Button:
            text:'next'
            on_release: app.root.current = "blank_page"

然后您可以访问该属性的 ID。

Python:

class MyApp(App):

    def build(self):

        self.now = datetime.now()
        Clock.schedule_interval(self.update_clock, 1)
        self.my_label = Label(text= self.now.strftime('%H:%M:%S'))
        return kv

    def update_clock(self, *args):
        self.now = self.now + timedelta(seconds = 1)
        self.root.bp.ids['my_label'].text = self.now.strftime('%H:%M:%S')
        print(self.now.strftime('%H:%M:%S'))

暂无
暂无

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

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