在 __init__ word=self.search_box.text AttributeError: 'NoneType' 对象没有属性 'text'

2024-04-12

我正在制作一个应用程序。据我所知,我做得正确,但仍然收到此错误

字=self.search_box.text AttributeError:“NoneType”对象没有属性“text”

我已经检查了拼写错误和其他常见错误,但它仍然不起作用。

这是代码-

import kivy
kivy.require('1.10.0')

from kivy.uix.stacklayout import StackLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label 
from kivy.app import App
from kivy.uix.popup import Popup  
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.lang import Builder 
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.properties import StringProperty


import json

Builder.load_file('VocabularyJournal.kv')

class MenuPage(Screen):
    pass

class DisplayPage(Screen):
    search_box= ObjectProperty()
    label_maening=StringProperty()
    label_synonym=StringProperty()
    label_ant=StringProperty()
    label_sentence=StringProperty()


    def __init__(self, **kwargs):
        super(DisplayPage,self).__init__(**kwargs)
        with open('vocab_words.json') as rfile:
            data=json.load(rfile)

        word=self.search_box.text               #the error occurred here 

        for value in data:
            if value['word']==word:
                self.label_maening=value['meaning']
                self.label_synonym=value['synonym']
                self.label_ant=value['antonyms']
                self.label_sentence=value['sentence']


class WordInsertPage(Screen):
    pass


class NewWordPage(Screen):
    word_box = ObjectProperty()
    meaning_box = ObjectProperty()
    synonym_box = ObjectProperty()
    ant_box = ObjectProperty()
    sentence_box = ObjectProperty()


    def saving_data(self):

        with open('vocab_words.json') as rfile:
            data=json.load(rfile)


        entry={'word': self.word_box.text, 'meaning': self.meaning_box.text, 'synonym': self.synonym_box.text, 'antonyms': self.ant_box.text, 'sentence': self.sentence_box.text}
        data.append(entry)


        with open('vocab_words.json','w') as wfile:
            json.dump(data,wfile,indent=4)


class FlashCard(Screen):
    pass

class WordGroups(Screen):
    pass

class Manager(ScreenManager):
    pass

class VocabularyJournalApp(App):
    def build(self):
        return Manager()

object = VocabularyJournalApp()
object.run()

这是 kv 代码-

<Manager>:
    MenuPage:
        name: 'menu'
    WordInsertPage:
        name: 'insertword'
    NewWordPage:
        name: 'newword'
    FlashCard:
        name: 'flashcard'
    WordGroups:
        name: 'wordgroup'
    DisplayPage:
        name: 'display'

<MenuPage>:
    Label: 
        text: "Vocabulary Journal"
        size_hint: .90,.10

    StackLayout:
        orientation: 'tb-rl'
        spacing: 10
        padding: 10

        Button:
            text: 'Search'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='insertword'
        Button:
            text: 'New Word'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='insertword'
        Button:
            text: 'Flash Cards'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='flashcard'

        Button:
            text: 'Word Groups'
            size_hint: None,.20
            width: 130
            background_down:'darkgrey.png'
            on_press: root.manager.current='wordgroup'

<WordInsertPage>:

    FloatLayout:

        Button: 
            text: "New Word"
            on_press: root.manager.current='newword'
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.3}
            background_down: 'darkgrey.png'
        Button:
            text: "search word"
            on_press: root.manager.current='display'
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.5}
            background_down: 'darkgrey.png'
        Button:
            text: 'Flash Cards'
            on_press: root.manager.current="flashcard"
            font_size: 30
            color: 0,0,0,1
            size_hint: .2, .1
            pos_hint: {"center_x": .5, "center_y": 0.7}
            background_down: 'darkgrey.png'



<NewWordPage>:
    id: refer_to_it
    word_box: word_input
    meaning_box: meaning_input
    synonym_box: Synonym_input
    ant_box: ant_input
    sentence_box: sentence_input
    StackLayout:
        orientation: 'tb-rl'
        spacing: 10
        padding: 90
        TextInput:
            text: "write your word here"
            color: 1,1,1,1
            id: word_input
            width: 300
            size_hint: None, .10

        TextInput:
            text: "write meaning of your word here"
            color: 1,1,1,1
            id: meaning_input
            width: 600
            size_hint: None, .20

        TextInput:
            text: "write Synonyms of your word here"
            color: 1,1,1,1
            id: Synonym_input
            width: 600
            size_hint: None, .20

        TextInput:
            text: "write antonyms of your text here"
            color: 1,1,1,1
            id: ant_input
            width: 600
            size_hint: None, .20

        TextInput:
            text: "write a sentence based on your word here"
            color: 1,1,1,1
            id: sentence_input
            width: 600
            size_hint: None, .20

        Button:
            text: 'Save'
            size_hint: None,.10
            width: 130
            background_down:'darkgrey.png'
            on_press: refer_to_it.saving_data()     

<DisplayPage>:
    search_box: search_text  # search_box is the reference to the textinput in py file
    BoxLayout:
        size_hint_y: None
        height: '48dp'

        TextInput:
            text:'enter the word you wanna search here'
            id: search_text


        ToggleButton:
            id: tog
            text: 'Horizontal'
            group: 'accordion'
            state: 'down'

        ToggleButton:
            text: 'Vertical'
            group: 'accordion'

    Accordion:
        orientation: 'horizontal' if tog.state == 'down' else 'vertical'    

        AccordionItem:
            title:'meaning'

            Label:
                text: root.label_maening
                text_size: self.width, None

        AccordionItem:
            title:'Synonym'

            Label:
                text: root.label_synonym
                text_size: self.width, None

        AccordionItem:
            title:'Antonym'

            Label:
                text: root.label_ant
                text_size: self.width, None

        AccordionItem:
            title:'Sentence'

            Label:
                text: root.label_sentence
                text_size: self.width, None

造成问题的原因是子级没有卡在父级的构造函数中,它稍后会执行此操作,因此构造函数中的 search_box 将是 None ,解决方案是在 Clock 的帮助下完成构造函数后立即执行它:

from kivy.clock import Clock


class DisplayPage(Screen):
    search_box= ObjectProperty()
    label_maening=StringProperty()
    label_synonym=StringProperty()
    label_ant=StringProperty()
    label_sentence=StringProperty()


    def __init__(self, **kwargs):
        super(DisplayPage,self).__init__(**kwargs)
        Clock.schedule_once(self.callback)

    def callback(self, dt):
        with open('vocab_words.json') as rfile:
            data=json.load(rfile)

        word=self.search_box.text               #the error occurred here 

        for value in data:
            if value['word']==word:
                self.label_maening=value['meaning']
                self.label_synonym=value['synonym']
                self.label_ant=value['antonyms']
                self.label_sentence=value['sentence']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 __init__ word=self.search_box.text AttributeError: 'NoneType' 对象没有属性 'text' 的相关文章

  • Flask中使用的路由装饰器是如何工作的

    我熟悉 Python 装饰器的基础知识 但是我不明白这个用于 Flask 路由的特定装饰器是如何工作的 以下是 Flask 网站上的代码片段 from flask import Flask escape request app Flask
  • 为什么我的混淆矩阵只返回一个数字?

    我正在做二元分类 每当我的预测等于事实时 我发现sklearn metrics confusion matrix返回单个值 难道没有问题吗 from sklearn metrics import confusion matrix print
  • 将 yerr/xerr 绘制为阴影区域而不是误差线

    在 matplotlib 中 如何将误差绘制为阴影区域而不是误差条 例如 而不是 忽略示例图中各点之间的平滑插值 这需要进行一些手动插值 或者只是获得更高分辨率的数据 您可以使用pyplot fill between https matpl
  • 如何删除 PyCharm 中的项目?

    如果我关闭一个项目 然后删除该项目文件夹 则在 PyCharm 重新启动后 会再次创建一个空的项目文件夹 只需按顺序执行以下步骤即可 他们假设您当前在 PyCharm 窗口中打开了该项目 单击 文件 gt 关闭项目 关闭项目 在 PyCha
  • 如何在python中附加两个字节?

    说你有b x04 and b x00 你如何将它们组合起来b x0400 使用Python 3 gt gt gt a b x04 gt gt gt b b x00 gt gt gt a b b x04 x00
  • 指示电子邮件的类型

    我有以下自动化程序 它将电子邮件发送给我自己 并添加了特定的链接 import win32com client as win32 import easygui import tkinter as to from tkinter import
  • 使用 Python 3 动态插入到 sqlite

    我想使用 sqlite 写入多个表 但我不想提前手动指定查询 有数十种可能的排列 例如 def insert sqlite tablename data list global dbc dbc execute insert into tab
  • python array(10,1) 和 array(10,) 之间的区别

    我正在尝试将 MNIST 数据集加载到数组中 当我使用 X train y train X test y test mnist load data 我得到一个数组 y test 10000 但我希望它的形状为 10000 1 数组 1000
  • 从 pyspark.sql 中的列表创建数据框

    我完全陷入了有线的境地 现在我有一个清单li li example data map lambda x get labeled prediction w x collect print li type li 输出就像 0 0 59 0 0
  • 在Python中读取tiff标签

    我正在尝试用 Python 读取 tiff 文件的标签 该文件是 RGB 的uint16每个通道的值 我目前正在使用tifffile import tifffile img tifffile imread file tif 然而 img是一
  • 在 Mac OSX 上从 Python 3.6 运行 wine 命令

    我正在尝试用 Python 编写一个打开的脚本wine然后发送代码到wine终端打开一个 exe程序 这 exe程序也是命令驱动的 我可以打开wine 但我无法进一步 import shlex subprocess line usr bin
  • 更改QLineEdit的ClearButton图标

    我想在Windows 10 1909 64位 上的Python 3 8和PyQt5 5 15 0 上更改我的QLineEdit的ClearButton图标 稍后我想在Linux上运行代码 我尝试应用此处找到的代码 如何在 QLineEdit
  • 时间序列数据预处理 - numpy strides 技巧以节省内存

    我正在预处理一个时间序列数据集 将其形状从二维 数据点 特征 更改为三维 数据点 时间窗口 特征 在这样的视角中 时间窗口 有时也称为回顾 指示作为输入变量来预测下一个时间段的先前时间步长 数据点的数量 换句话说 时间窗口是机器学习算法在对
  • 使用标签或 href 传递 Django 数据

    我有一个包含链接的表 当单击该链接进行更多操作时 我想将一些数据传递给我的函数 my html table tbody for query in queries tr td value a href internal my func que
  • pygame:使用 sprite.RenderPlain 绘制精灵组的顺序

    我有一个精灵组 需要按一定的顺序绘制 以便其精灵按应有的方式重叠 然而 即使使用运算符模块函数 sorted self sprites key attrgetter y x 对组进行排序 顺序也是错误的 我该如何解决这个问题 直截了当地说
  • Discord.py 嵌入中禁用按钮/冻结按钮

    I m trying to make a replica of this bot in which when I press any of the buttons below it shows a dropdown menu and you
  • 使用 selenium 和 python 来提取 javascript 生成的 HTML?萤火虫?

    这里是Python新手 我遇到的是数据收集问题 我在这个网站上 当我用 Firebug 检查我想要的元素时 它显示了包含我需要的信息的源 然而常规源代码 没有 Firebug 不会给我这个信息 这意味着我也无法通过正常的 selenium
  • 仅允许正小数

    在我的 Django 模型中 我创建了一个如下所示的小数字段 price models DecimalField u Price decimal places 2 max digits 12 显然 价格为负或零是没有意义的 有没有办法将小数
  • 在 numpy 中连接维度

    我有x 1 2 3 4 5 6 7 8 9 10 11 12 shape 2 2 3 I want 1 2 3 4 5 6 7 8 9 10 11 12 shape 2 6 也就是说 我想连接中间维度的所有项目 在这种特殊情况下我可以得到这
  • 如何在 scikit 中加载 CSV 数据并将其用于朴素贝叶斯分类

    尝试加载自定义数据以在 Scikit 中执行 NB 分类 需要帮助将示例数据加载到 Scikit 中 然后执行 NB 如何加载目标的分类值 使用相同的数据进行训练和测试 或使用完整的数据集进行测试 Sl No Member ID Membe

随机推荐