如何解决如何解决在 Kivy 中 NoneType 对象没有属性“文本”的问题?
.py
class dfpScreen(Screen):
inputdf = ObjectProperty(None)
inp = ObjectProperty(None)
#d = Stringproperty()
#pval = Stringproperty()
def getvalue(self):
d=float(str(self.inputdf.text))
print(d)
.kv
<dfpScreen>:
name:'dfp'
GridLayout:
inputdf:inputdf
inp:inp
cols:2
Label:
text: "Df (Degree of Freedom):"
TextInput:
id:inputdf
multiline:False
Label:
text: "P value:"
TextInput:
id:inp
multiline:False
Button:
text: "Back to Input"
on_release:
app.root.current='mainwin'
root.manager.transition.direction = 'right'
Button:
text: "Check"
on_release:
root.getvalue()
root.ibtn()
app.root.current='calcwin'
root.manager.transition.direction = 'left'
当按钮被按下时,它应该读取文本输入框中的文本,但它不起作用。我有类似的代码,他们工作。我不知道为什么这个没有。这是我制作的一个类似的代码。
class MainScreen(Screen):
ans=Stringproperty()
adding = ObjectProperty(None)
adding2 = Stringproperty()
deleting = ObjectProperty(None)
def btn(self):
try:
lis.append(float(self.adding.text))
#print(lis)
except:
pass
<MainScreen>:
id:first
name:'mainwin'
adding:adding
adding3:adding3
FloatLayout:
TextInput:
id: adding
multiline:False
size_hint: 0.3,0.1
pos_hint: {"x": 0.2,"y":0.75}
font_size: (40)
这两个代码有什么区别? 谢谢,
附言 它解决了。我不知道为什么我没有尝试这个:
on_release:
root.getvaluebtn(inputdf.text)
感谢您的帮助。
解决方法
缺少调用的 init。如果您正在为类创建对象,则需要初始化变量。或者有一个 api 来初始化。
class dfpScreen(Screen):
def __init__ (self) :
self.inputdf = ObjectProperty(None)
self.inp = ObjectProperty(None)
def getvalue(self):
if self.inputdf != None:
d=float(str(self.inputdf.text))
print(d)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。