Python 3.x Kivy TextInput字段抛出缺少背景色错误
我试图熟悉一些基本的Kivy GUI构建,并尝试创建一个文本字段,用户输入一些文本,当他们按Return时,它会更改标签 我的代码:Python 3.x Kivy TextInput字段抛出缺少背景色错误,python-3.x,kivy,Python 3.x,Kivy,我试图熟悉一些基本的Kivy GUI构建,并尝试创建一个文本字段,用户输入一些文本,当他们按Return时,它会更改标签 我的代码: from kivy.app import App from kivy.uix.textinput import TextInput from kivy.uix.floatlayout import FloatLayout from kivy.uix.label import Label class textInput(FloatLayout): d
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
class textInput(FloatLayout):
def __init__(self, **kwargs):
super(textInput, self).__init__(**kwargs)
self.label = Label(text = 'No text')
self.textSpace = TextInput(pos = (300,300), text = 'Type here', multiline = False, on_enter = changeText())
self.orientation = 'horizontal'
self.size = (450,300)
self.add_widget(label)
self.add_widget(textSpace)
def changeText(value):
self.textSpace.text = value
class myApp(App):
def build(self):
return textInput()
if __name__ == '__main__':
myApp().run()
尝试从终端执行代码时,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/kivy/lang.py", line 1649, in create_handler
return eval(value, idmap)
File "/usr/local/lib/python3.5/site-packages/kivy/data/style.kv", line 167, in <module>
rgba: self.background_color
File "kivy/weakproxy.pyx", line 19, in kivy.weakproxy.WeakProxy.__getattr__ (/var/folders/yx/bvym8vps2sd_0q19bxy2ddfw0000gn/T/pip-xjpmjrk9-build/kivy/weakproxy.c:1101)
AttributeError: 'textInput' object has no attribute 'background_color'
回溯(最近一次呼叫最后一次):
文件“/usr/local/lib/python3.5/site packages/kivy/lang.py”,第1649行,在create\u处理程序中
返回值(值,idmap)
文件“/usr/local/lib/python3.5/site packages/kivy/data/style.kv”,第167行,in
rgba:self.background\u颜色
文件“kivy/weakproxy.pyx”,第19行,在kivy.weakproxy.weakproxy.\uuu getattr\uuuu(/var/folders/yx/bvym8vps2sd_0q19bxy2dfw0000gn/T/pip-xjpmjrk9-build/kivy/weakproxy.c:1101)中
AttributeError:“textInput”对象没有“背景颜色”属性
我试图找到一些关于TextInput的教程,但没有一个在小部件上设置背景颜色
我试图给它一个背景色,但没有成功。我尝试了不同的布局类型(网格布局),但结果相同。我认为您存在名称冲突问题-我不确定原因,但您的textInput正在尝试应用textInput规则。也许那里的某个地方有一个
.lower()
。您可以直接重命名该类,但使用相同的名称(根据大小写)并不是一种好的样式
您也会遇到一些问题,例如,当没有变量
label
时,self.add_widget(label)
,因为您的意思是self.label
,出于某种原因,类名textInput
会导致问题。类名无论如何都应该大写,我建议在您选择的名称前面加上“我的”,那么MyTextWidget
呢
第二个主要问题是:对于TextInput
小部件,没有on\u enter
事件。很明显,您阅读了有关TextInput的文档,因为这些文档为TextInput的“验证上的<代码>事件”(当您点击Enter键时触发)分配了一个函数,并且文档用于其事件处理程序函数的名称为…on\ Enter。最令人困惑的是,事件名称在\u text\u validate上是,因此需要将函数分配给该事件。文档应该将其事件处理程序函数命名为类似于my\u func
,而不是on\u enter
此外,当您将函数分配给事件时,您不是调用该函数的人,而是kivy将在将来某个时间调用该函数,因此您不希望这样做:
def do_stuff():
return 'hello'
on_text_validate = do_stuff() #Calls function now!
原因是:代码中的所有函数调用都被函数的返回值替换(如果没有return语句,则函数默认不返回任何值),因此上述内容相当于编写:
on_text_validate = "hello"
稍后,kivy将调用在_text _validate
上分配给的任何内容,就好像它是一个函数一样:
on_text_validate(..)
这将产生错误:
TypeError: 'str' object is not callable
您要做的是:只将函数名
分配给on\u text\u validate
:
def do_stuff():
print 'hello'
on_text_validate = do_stuff #Function does not execute without the trailing: ()
#Time passes...
on_text_validate(...) #kivy will do this after the user hits Enter in the TextInput
--output:--
hello
这只是普通的python,这意味着执行以下操作是正常的:
def do_stuff():
print 'hello'
f = do_stuff
f() #=> hello
更有用的是:
+-----+ +-----+
func=greet | | x="hello" | |
V | V |
def do_stuff(func): | def show(x): |
func() | print x |
print 'world' | |
| |
def greet(): | |
print 'hello' | |
| +-----+
+--------+ |
| |
do_stuff(greet) show('hello')
--output:- --output:--
hello hello
world
正如@increment指出的,您的代码还有其他问题。见以下评论:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
class MyTextWidget(FloatLayout): #Capitalization here
def __init__(self, **kwargs):
super(MyTextWidget, self).__init__(**kwargs) #Capitalization here
self.label = Label(text = 'No text')
self.textSpace = TextInput(
pos = (20, 20), #The top of the TextInput is off the screen
text = 'Type here',
multiline = False,
on_text_validate = self.changeText #You assign the function name here
)
self.orientation = 'horizontal'
self.size = (450,300)
self.add_widget(self.label) #Nowhere in your code is there a variable
#named label
self.add_widget(self.textSpace) #Nowhere in your code is there
#a variable named textSpace
def changeText(self, textInput):
self.textSpace.text = textInput.text + ": is what you entered"
class myApp(App):
def build(self):
return MyTextWidget()
if __name__ == '__main__':
myApp().run()
请注意,由于不调用指定给事件的函数,因此无法指定参数。例如,如果指定参数
on_validate_text = do_stuff('hello', 10)
然后该函数立即执行,并将函数的返回值分配给on_validate_text。这就提出了一个问题:你应该如何定义你的函数?需要一个论点吗?两个论点?kivy将这样调用您的函数:
on_validate_text(arg1, arg2..., argn)
因此,您必须阅读文档以了解kivy在调用事件处理程序函数时将指定多少个参数。或者,如果文件不充分,您可以这样做:
def do_stuff(*args):
for arg in args:
print arg
...
...
on_text_validate = do_stuff
然后检查输出。就是这样,我感谢您的评论!他们的文档使用“on_enter”作为函数名,这让人非常困惑,你说得对,这就是我的原因。再次感谢!