![](/img/trans.png)
[英]AttributeError: 'NoneType' object has no attribute 'text' kivy
[英]Python-Kivy AttributeError: 'NoneType' object has no attribute 'text'
我剛剛開始編碼,所以這里可能有很多編碼錯誤。 我正在嘗試開發一個有助於我個人計費的應用程序。 我希望這個應用程序首先顯示發票# 和 for(目的) 的文本框條目,然后進入滾動頁面,其中將顯示所有承運人名稱供用戶選擇,然后是下一頁(布局類似於第一個具有成本文本框,以及 local(城市名稱列表)、outcal(相同城市名稱列表)、Driver(駕駛員名稱列表)、Dispatcher(調度員名稱列表)的下拉列表。
最后,我希望所有這些信息都存儲在一個文本文檔中,以便我可以設計代碼來解釋它並將其放置在 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
}
對於尚未集成的名稱的 rest,我得到了這個無值錯誤:
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
您正在嘗試獲取For
文本,但 TOPRIGHTINVOICE 內部沒有名為“for”的TOPRIGHTINVOICE
。 所以這就是你的錯誤來自哪里。 您可以簡單地將其更改為:
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"
...
它會起作用。
但我不得不說你不需要為每個文本輸入創建一個新的 object。 這可能會占用 memory 並導致類似的混亂。 您已經將id
傳遞給輸入,所以讓我們使用它:
首先刪除這個:
invoice = ObjectProperty(None)
purpose = ObjectProperty(None)
這也在你的 kv 文件中:
invoice: invoice
purpose: purpose
然后您可以使用self.ids.yourID
來引用 object。 像那樣:
def next(self):
db.add_loadinfo(self.ids.invoice.text, self.ids.purpose.text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.