Kivy/PyMongo:使用 TextInput 中的 'enter' 键将输入的注释输入数据库
Kivy/PyMongo: Using 'enter' key in TextInput to feed entered note into database
我正在尝试制作一个应用程序的一部分,基本上人们可以为自己留下简短的单行提醒。我想确保当此人按下 'enter' 时,便条会保存到数据库中。
这是我在 main.py 中的代码:
class Reminders(Screen):
collection = db['Notes']
note_text = TextInput()
note_num = collection.count_documents({})
def add_note(self):
self.note_text = TextInput(text='Note', size_hint=(0.2, 0.25), multiline=False, on_text_validate=self.keyboard_down)
self.ids.layout.add_widget(self.note_text)
def keyboard_down(self, **kwargs):
entry = {'_id': (self.note_num + 1), 'Note': self.note_text.text}
self.collection.insert_one(entry)
my.kv:
FloatLayout:
Label:
text: "Reminders"
color: (0, 0.702, 0, 1)
font_size: 50
pos_hint: {'x': 0.1, 'top': 1}
size_hint: 0.2, 0.2
Button:
text: 'Add'
size_hint: 0.07, 0.07
pos_hint: {'x': 0.78,'top': 0.94}
on_release: root.add_note()
ScrollView:
size_hint: 0.96, 0.6
pos_hint: {'x': 0.02, 'top': 0.7}
StackLayout:
id: layout
orientation: 'tb-lr'
(以上都是一小段代码,kv代码在标签下)
我收到一个类型错误 >TypeError: keyboard_down() takes 1 positional argument but 2 were given
(实际按回车时出现错误)。
def keyboard_down(self, **kwargs):
您的函数定义采用一个位置参数,您将其命名为 self
。该错误告诉您它被传递了两个位置参数。您必须更改您的定义以匹配它的调用方式,例如
def keyboard_down(self, _, **kwargs):
我正在尝试制作一个应用程序的一部分,基本上人们可以为自己留下简短的单行提醒。我想确保当此人按下 'enter' 时,便条会保存到数据库中。
这是我在 main.py 中的代码:
class Reminders(Screen):
collection = db['Notes']
note_text = TextInput()
note_num = collection.count_documents({})
def add_note(self):
self.note_text = TextInput(text='Note', size_hint=(0.2, 0.25), multiline=False, on_text_validate=self.keyboard_down)
self.ids.layout.add_widget(self.note_text)
def keyboard_down(self, **kwargs):
entry = {'_id': (self.note_num + 1), 'Note': self.note_text.text}
self.collection.insert_one(entry)
my.kv:
FloatLayout:
Label:
text: "Reminders"
color: (0, 0.702, 0, 1)
font_size: 50
pos_hint: {'x': 0.1, 'top': 1}
size_hint: 0.2, 0.2
Button:
text: 'Add'
size_hint: 0.07, 0.07
pos_hint: {'x': 0.78,'top': 0.94}
on_release: root.add_note()
ScrollView:
size_hint: 0.96, 0.6
pos_hint: {'x': 0.02, 'top': 0.7}
StackLayout:
id: layout
orientation: 'tb-lr'
(以上都是一小段代码,kv代码在
我收到一个类型错误 >TypeError: keyboard_down() takes 1 positional argument but 2 were given
(实际按回车时出现错误)。
def keyboard_down(self, **kwargs):
您的函数定义采用一个位置参数,您将其命名为 self
。该错误告诉您它被传递了两个位置参数。您必须更改您的定义以匹配它的调用方式,例如
def keyboard_down(self, _, **kwargs):