+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python-Kivy AttributeError:“ NoneType”对象没有属性“ text”

发布于2021-03-05 09:50     阅读(698)     评论(0)     点赞(25)     收藏(1)


0

1

2

3

4

我刚刚开始编码,因此这里可能存在很多编码错误。我正在尝试开发一个可以帮助我进行个人结算的应用程序。我希望这个应用程序首先显示发票编号和(目的)的文本框条目,然后进入滚动页面,在该页面上将显示所有承运人名称供用户选择,然后显示下一页(布局与第一个具有用于成本的文本框,以及一个下拉列表,用于显示本地(城市名称列表),outcal(城市名称列表),驱动程序(驱动程序名称列表),调度程序(调度程序名称列表)。

最后,我希望所有这些信息都存储在文本文档中,以便我可以设计代码来解释并将其放置在excel文件的指定部分中。Kivy Exel有这一切可能吗?

该应用程序将打开,我们可以在第一个第一个屏幕上输入文本,但是一旦单击下一个按钮就会崩溃。这是代码和错误代码。

数据库错误代码:

{
    "resource": "/c:/Users/CJ/Desktop/my app/screens/exel.py",
    "owner": "python",
    "code": "undefined-variable",
    "severity": 8,
    "message": "Undefined variable 'invoice'",
    "source": "pylint",
    "startLineNumber": 33,
    "startColumn": 56,
    "endLineNumber": 33,
    "endColumn": 56
}

main.py的错误代码

{
    "resource": "/c:/Users/CJ/Desktop/my app/screens/Screens.py",
    "owner": "python",
    "code": "no-name-in-module",
    "severity": 8,
    "message": "No name 'ObjectProperty' in module 'kivy.properties'",
    "source": "pylint",
    "startLineNumber": 6,
    "startColumn": 1,
    "endLineNumber": 6,
    "endColumn": 1
}

对于尚未集成的其余名称,我得到了这个没有值的错误:

No value for argument 'carrier' in method call
No value for argument 'carrier' in method call

代码:

# main.py
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivymd.uix.list import MDList,OneLineListItem
from kivy.uix.scrollview import ScrollView
from exel import DataBase

class TOPRIGHTINVOICE(Screen):
    invoice = ObjectProperty(None)
    For = ObjectProperty(None)

    def next(self):
        db.add_loadinfo(self.invoice.text, self.For.text)

        self.reset()
        sm.current = "CARRIERPAGE"
    
    def reset(self):
        self.invoice.text = "" 
        self.For.text = ""

    def login(self):
        self.reset()
        sm.current = "CARRIERPAGE"


class CARRIERPAGE(Screen):

    def build(self):
        screen = Screen()

        scroll = ScrollView()
        list_view= MDList()
        scroll.add_widget(list_view)

        item1 = OneLineListItem(text='Coyote')
        item2 = OneLineListItem(text='TQL')

        list_view.add_widget(item1)
        list_view.add_widget(item2)

        screen.add_widget(list_view)
     


def MissingInfo():
    pop = Popup(title= 'Missing Information',
                content=Label(text='You are missing information, are you sure you want to continue?'),
                suze_hint=(None,None), size=(400,400))
    pop.open()

class WindowManager(ScreenManager):
    pass

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

sm = WindowManager()
db = DataBase("user.txt")

screens = [TOPRIGHTINVOICE(name="main"), CARRIERPAGE(name="CARRIERPAGE")]
for screen in screens:
    sm.add_widget(screen)

sm.current = "main"

class MyScreen(App):
    def build(self):
        return sm

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


#my.kv
<TOPRIGHTINVOICE>:
    name: "main"

    invoice: invoice
    purpose: purpose
        

    FloatLayout:
        cols:1

        FloatLayout:
            size: root.width, root.height/2


            Label:
                size_hint: 0.5,0.12
                pos_hint: {"x":0, "top":0.8}
                text: "Invoice #: "
                font_size: dp(20)

            TextInput:
                pos_hint: {"x":0.5, "top":0.8}
                size_hint: 0.4, 0.12
                id: invoice
                multiline: False
                font_size: dp(20)

            Label:
                size_hint: 0.5,0.12
                pos_hint: {"x":0, "top":0.8-0.13}
                text: "For: "
                font_size: dp(20)

            TextInput:
                pos_hint: {"x":0.5, "top":0.8-0.13}
                size_hint: 0.4, 0.12
                id: purpose
                multiline: False
                font_size: dp(20)

        Button:
            pos_hint:{"x":0.3,"y":0.25}
            size_hint: 0.4, 0.1
            font_size: dp(20)
            text: "Next"
            on_release:
                root.manager.transition.direction = "left"
                root.next()


            
            



<CARRIERPAGE>:
    name: "CARRIERPAGE"
    
    Button:
        id:Back
        text: "Back"
        on_release: 
            app.root.current = "main"
            root.manager.transition.direction = "right"
#exel database
import xlrd, xlwt
from xlutils.copy import copy
import datetime

class DataBase:
    def __init__(self, filename):
        self.filename = filename
        self.loadinfo = None
        self.file = None
        self.load()

    def load(self):
        self.file = open(self.filename, "r")
        self.loadinfo = {}

        for line in self.file:
            invoice, purpose, carrier, cost, local, outcal, Driver, Dispatcher = line.strip().split(";")
            self.loadinfo[invoice] = (invoice, purpose, carrier, cost, local, outcal, Driver, Dispatcher)

        self.file.close()

    def top_right(self, invoice, purpose):
        pass

    def add_loadinfo(self, invoice, purpose, carrier, cost, local, outcal, Driver, Dispatcher):
        self.loadinfo[invoice.strip()] = (invoice.strip(), purpose.strip(), carrier.strip(), cost.strip(), local.strip(), outcal.strip(), Driver.strip(), Dispatcher.strip(), DataBase.get_date())
        self.save()

    
    def save(self):
        with open(self.filename, "w") as f:
            for loadinfo in self.loadinfo:
                f.write(loadinfo + ";" + self.loadinfo[invoice][0] + ";" + self.loadinfo[invoice][1] + ";" + self.loadinfo[invoice][2] + "\n")

    @staticmethod
    def get_date():
        return str(datetime.datetime.now()).split(" ")[0]
        

#code to put it into excel

# read_book = xlrd.open_workbook("Invoice.xls", formatting_info=True) #Make Readable Copy
# write_book = copy(read_book) #Make Writeable Copy

# write_sheet1 = write_book.get_sheet(1) #Get sheet 1 in writeable copy
# write_sheet1.write(1, 11, self.invoice.text) #Write 'test' to cell (1, 11)

# write_sheet2 = write_book.get_sheet(2) #Get sheet 2 in writeable copy
# write_sheet2.write(3, 14, '135') #Write '135' to cell (3, 14)

# write_book.save("New/File/Path") #Save the newly written copy. Enter the same as the old path to write over

解决方案


You are trying to get the For text but there is no object called "for" inside TOPRIGHTINVOICE. So that's where your erros are coming from. You can simply change it to:

class TOPRIGHTINVOICE(Screen):
    invoice = ObjectProperty(None)
    purpose = ObjectProperty(None)

    def next(self):
        db.add_loadinfo(self.invoice.text, self.purpose.text)
        
        self.reset()
        sm.current = "CARRIERPAGE"

    def reset(self):
        self.invoice.text = ""
        self.purpose.text = ""

    def login(self):
        self.reset()
        sm.current = "CARRIERPAGE"
...

And it will work.

But i have to say that you don't need to create a new object for every text input. This can take up memory and lead to confusions like that one. You're already passing an id to the input, so lets use it:

First delete this:

    invoice = ObjectProperty(None)
    purpose = ObjectProperty(None)

And also this on your kv file:

    invoice: invoice
    purpose: purpose

And then you can just use self.ids.yourID to referenciate the object. Like that:

    def next(self):
        db.add_loadinfo(self.ids.invoice.text, self.ids.purpose.text)

0

1

2

3

4

5



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/879505/768247c3fdf378cbe703/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

25 0
收藏该文
已收藏

评论内容:(最多支持255个字符)