繁体   English   中英

Kivy - ScreenManager 说它不识别另一个屏幕名称,尽管它在那里?

[英]Kivy - ScreenManager saying it does not recognize another screen name despite it being there?

所以,我想创建一个在 kivy 中创建任务/习惯的应用程序。 基本上,会有一个屏幕显示您的任务/习惯,您可以单击一个按钮将您带到另一个屏幕以创建新的任务/习惯。 您将在该屏幕中输入有关任务/习惯的信息,然后单击提交按钮。 提交按钮会将任务/习惯添加到第一个屏幕。 但是,当我运行我的代码时,它第一次运行。 我可以转到第二个屏幕,添加我的习惯/代码,然后返回第一个屏幕。 但是,当我单击第一个屏幕上的按钮时,它给了我一个错误。

错误是:kivy.uix.screenmanager.ScreenManagerException:没有名称为“second”的屏幕。

这是我的代码:

主要.py:

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.image import Image
from PIL import Image
import numpy as np

class FirstWindow(Screen):
    def add_label(self, name, time, date):
        label = Label(text= f' {name}, {time}, {date}')
        self.ids.boxlayout.add_widget(label)


class SecondWindow(Screen):
    a_task = False
    a_habit = False
    name = ""
    time = ""
    date = ""

    def task_button(self):
        self.a_task = True
        self.a_habit = False


    def habit_button(self):
        self.a_habit = True
        self.a_task = False


    def submit_button(self):
        self.name = self.ids.the_name.text
        self.time = f'{self.ids.time_spinner_1.text}:{self.ids.time_spinner_2.text}'
        self.date = f'{self.ids.date_spinner_1.text}/{self.ids.date_spinner_2.text}'


    def get_name(self):
        return self.name

    def get_time(self):
        return self.time

    def get_date(self):
        return self.date


kv = Builder.load_file('Button.kv')


class ButtonApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(FirstWindow())
        sm.add_widget(SecondWindow())
        return sm

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

按钮.kv:

<FirstWindow>:
    name: "first"
    BoxLayout:
        id: boxlayout
        orientation: "vertical"
        canvas.before:
            Color:
                rgba: (0.3,0.33,0.3,1)
            Rectangle:
                pos: self.pos
                size: self.size
        FloatLayout:
            Label:
                text: "To Do List"
                font_size: 32
                pos_hint: { 'right': 0.65, 'top': 1.4 }
        Button:
            text: "add a task"
            on_release: root.manager.current = "second"

<SecondWindow>:
    name: "second"
    BoxLayout:
        orientation: "vertical"
        spacing: "10dp"
        canvas.before:
            Color:
                rgba: (0.3,0.33,0.3,1)
            Rectangle:
                pos: self.pos
                size: self.size
        FloatLayout:
            Label:
                text: "Add A Task"
                font_size: 32
                pos_hint: { 'right': 0.65, 'top': 1.2 }
        BoxLayout:
            orientation: "horizontal"
            Button:
                text: "task"
                on_press: root.task_button()
            Button:
                text: "habit"
                on_press: root.habit_button()

        TextInput:
            id: the_name
            text: "Name"

        Label:
            text: "alert"

        BoxLayout:
            orientation: "horizontal"
            Spinner:
                id: time_spinner_1
                text: "00"
                values: ['1','2','3','4','5','6','7','8','9','10','11', '12']

            Spinner:
                id: time_spinner_2
                text: "00"
                values: ['01','02','03','04','05','06','07','08','09','10','11', '12', '13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59']

        BoxLayout:
            orientation: "horizontal"
            Spinner:
                id: date_spinner_1
                text: "00"
                values: ['1','2','3','4','5','6','7','8','9','10','11', '12']

            Spinner:
                id: date_spinner_2
                text: "00"
                values: ['01','02','03','04','05','06','07','08','09','10','11', '12', '13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']



        Button:
            text: "submit"
            on_press: root.submit_button()
            on_release:
                root.manager.current = "first"
                root.manager.get_screen("first").add_label(root.get_name(), root.get_time(), root.get_date())

你的代码:

def submit_button(self):
    self.name = self.ids.the_name.text
    self.time = f'{self.ids.time_spinner_1.text}:{self.ids.time_spinner_2.text}'
    self.date = f'{self.ids.date_spinner_1.text}/{self.ids.date_spinner_2.text}'

更改SecondWindow Screenname属性,因此尝试将Screen更改为second将不再起作用。 看起来您正试图将name用于其他目的。 我建议您使用具有不同名称的变量。

暂无
暂无

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

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