问题
I have a error in my code, I want to get or to print the product text but when i run it it gave me an error.
here is my code:
import kivy
from kivy.properties import ObjectProperty
from selenium import webdriver
import requests
from selectorlib import Extractor
from selenium.webdriver.common.keys import Keys
from kivymd.app import MDApp
from kivy.app import App
from kivy.lang import Builder
KV = '''
BoxLayout:
product: product
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDTextFieldRound:
id: product
hint_text: 'Enter a product'
icon_left: 'magnify'
on_text_validate: app.System()
'''
class Main(MDApp):
def build(self):
return Builder.load_string(KV)
product = ObjectProperty(None)
def System(self):
print(self.product.text)
if __name__ == "__main__":
Main().run()
it always gave me the next error
File "C:/Users/Yesnia/Desktop/PYTHON/Apps development/App/App_Checking_Store.py", line 34, in
System
print(self.product.text)
AttributeError: 'NoneType' object has no attribute 'text'
help me please
回答1:
So you build your kv string but have no way to reference it. There are many ways to mend the situation, heres an example.
KV = '''
BoxLayout:
###product: product ### this does not achieve anything, you're just giving
#some generic box layout a product attribute. this does not assign the App attribute.
size_hint: .8, .8
pos_hint: {"center_x": .5, "center_y": .5}
spacing: dp(100)
orientation: "vertical"
MDTextFieldRound:
id: product
hint_text: 'Enter a product'
icon_left: 'magnify'
on_text_validate: app.System()
'''
class Main(MDApp):
product = ObjectProperty(None)
my_kv = None
def build(self):
self.my_kv = Builder.load_string(KV)
self.product = self.my_kv.ids.product
return self.my_kv
def System(self):
print(self.product.text)
notice the comment i've left in your kv string, the addition of the new my_kv attribute to your App class, and the changes I've made to your build function.
Hope this helps!
来源:https://stackoverflow.com/questions/60138404/attributeerror-nonetype-on-the-selenium