Kivy 1.10.0 并使用kv文件作为我的Kivy语言。
Python 3.6
我有一个适用于mysql3的应用程序。该应用程序的一个页面允许我向数据库添加“闪存卡”。添加闪存卡时,其中一个表单元素使用Multiselect Spinner添加一个或多个标记,然后将此数据保存到数据库中。我第一次访问此页面时,我可以使用微调器并按预期添加所有内容。然后,如果我回去尝试添加第二张闪存卡,即使表格被清除,一切看起来与我第一次添加闪存卡的情况完全一样,微调器也会扼杀。数据似乎存储在某处,如第三个gif中所示,并以某种方式加载回微调器数据。 ('甲板'旋转器的问题是一样的。)
这个gif显示我第一次添加新卡时它正常工作:
这个gif显示我第二次添加新卡(无论我在添加两张卡之间做了什么,除了完全退出应用程序之外):
此gif显示数据似乎保留:
因为代码很长,我只会添加我认为相关的内容。如果缺少某些东西,请告诉我,我会立即添加。
class NewFlashCardScreen(Screen):
data = DictProperty({})
data_fc_tag_ids = DictProperty({})
data_fc_deck_ids = DictProperty({})
__events__ = ('on_submit',)
fc_tag_spinner_list = ListProperty(None)
fc_deck_spinner_list = ListProperty(None)
def on_parent(self, widget, parent):
gd = App.get_running_app()
self.fc_tag_spinner_list = []
fc_tag_list = Queries.get_fc_tag_list()
if len(fc_tag_list) > 0:
for fc_tag in fc_tag_list:
self.fc_tag_spinner_list.append(fc_tag[1])
self.data_fc_tag_ids[fc_tag[1]] = fc_tag[0]
else:
self.fc_tag_spinner_list.append("")
self.fc_deck_spinner_list = []
fc_deck_list = Queries.get_fc_deck_list()
if len(fc_deck_list) > 0:
for fc_deck in fc_deck_list:
self.fc_deck_spinner_list.append(fc_deck[1])
self.data_fc_deck_ids[fc_deck[1]] = fc_deck[0]
else:
self.fc_deck_spinner_list.append("")
def get_spinner_lists(self):
pass
def on_submit(self, data):
gd = App.get_running_app()
self.data['orig'] = gd.glob_dict['orig']
title = data['fc_title'] if data['fc_title'] else " "
front = data['fc_front'] if data['fc_front'] else " "
back = data['fc_back'] if data['fc_back'] else " "
difficulty = data['fc_difficulty'] if data['fc_difficulty'] else 0
self.fc_id = MiscFuns.get_id(16)
try:
c.execute("""
INSERT INTO `tbl_learning_flash_cards` (`fc_id`,`fc_title`,`fc_front`,`fc_back`,`fc_difficulty`)
VALUES (?,?,?,?,?)
""", (self.fc_id, title, front, back, difficulty))
conn.commit()
except sqlite3.Error as e:
print("An error occurred:", e.args[0])
class MultiSelectSpinner(Button):
dropdown = ObjectProperty(None)
values = ListProperty([])
selected_values = ListProperty([])
def __init__(self, **kwargs):
self.values.clear()
self.selected_values.clear()
self.bind(dropdown=self.update_dropdown)
self.bind(values=self.update_dropdown)
super(MultiSelectSpinner, self).__init__(**kwargs)
self.bind(on_release=self.toggle_dropdown)
def toggle_dropdown(self, *args):
if self.dropdown.parent:
self.dropdown.dismiss()
else:
self.dropdown.open(self)
def update_dropdown(self, *args):
if not self.dropdown:
self.dropdown = DropDown()
values = self.values
if values:
if self.dropdown.children:
self.dropdown.clear_widgets()
for value in values:
b = Factory.MultiSelectOption(text=value)
b.bind(state=self.select_value)
self.dropdown.add_widget(b)
def select_value(self, instance, value):
if value == 'down':
if instance.text not in self.selected_values:
self.selected_values.append(instance.text)
else:
if instance.text in self.selected_values:
self.selected_values.remove(instance.text)
def on_selected_values(self, instance, value):
if value:
self.text = ';'.join(value)
else:
self.text = ''
和kv文件:
<NewFlashCardScreen>:
BoxLayout:
orientation: "vertical"
BoxLayout:
size_hint_y: 0.1
Label:
size_hint_x: 0.2
text: "Card Title"
text_size: self.text_size
valign: 'middle'
TextInput:
id: new_fc_title
multiline: False
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
text: root.data['fc_title'] if 'fc_title' in root.data else ""
on_text: root.data['fc_title'] = self.text
Label:
size_hint_x: 0.2
text: "Difficulty"
text_size: self.text_size
valign: 'middle'
BoxLayout:
size_hint_y: 0.1
Label:
size_hint_x: 0.2
text: "Card Tag(s)"
text_size: self.text_size
valign: 'middle'
BoxLayout:
MultiSelectSpinner:
id: new_fc_tag
text: root.data['fc_tags'] if 'fc_tags' in root.data else "Select tag(s)"
values: root.fc_tag_spinner_list
on_text: root.data['fc_tags'] = self.text
Button:
size_hint_x: 0.3
text: "New Tag"
on_release: lib.Navigation.page_nav(dest='new_flash_card_tag', orig='new_flash_card', edit=False)
BoxLayout:
size_hint_x: 0.2
TextInput:
padding_y: [self.height / 2.0 - (self.line_height / 2.0) * len(self._lines), 0]
id: new_fc_difficulty
text: root.data['fc_difficulty'] if 'fc_difficulty' in root.data else "0"
on_text: root.data['fc_difficulty'] = self.text
BoxLayout:
size_hint_y: 0.1
Label:
size_hint_x: 0.2
text: "Card Deck(s)"
text_size: self.text_size
valign: 'middle'
BoxLayout:
MultiSelectSpinner:
id: new_fc_deck
text: root.data['fc_decks'] if 'fc_decks' in root.data else "Select deck(s)"
values: root.fc_deck_spinner_list
on_text: root.data['fc_decks'] = self.text
Button:
size_hint_x: 0.3
text: "New Deck"
on_release: lib.Navigation.page_nav(dest='new_flash_card_deck', orig='new_flash_card', edit=False)
BoxLayout:
size_hint_x: 0.2
BoxLayout:
orientation: "vertical"
Label:
size_hint_y: 0.1
text: "Card Front"
BoxLayout:
TextInput:
id: new_fc_front
multiline: True
text: root.data['fc_front'] if 'fc_front' in root.data else ""
on_text: root.data['fc_front'] = self.text
RstDocument:
text: new_fc_front.text
show_errors: True
BoxLayout:
orientation: "vertical"
Label:
size_hint_y: 0.1
text: "Card Back"
BoxLayout:
TextInput:
id: new_fc_back
multiline: True
text: root.data['fc_back'] if 'fc_back' in root.data else ""
on_text: root.data['fc_back'] = self.text
RstDocument:
text: new_fc_back.text
show_errors: True
BoxLayout:
size_hint_y: 0.2
Button:
text: "Cancel"
on_release: lib.Navigation.page_nav(dest='prev_page', orig='new_flash_card', edit=False)
Button:
text: "Submit"
on_release: root.dispatch('on_submit', root.data)
我已经尝试清除字典文件,self.data,self.data_fc_tag_ids和self.data_fc_deck_ids,但这没有帮助。
我不知道保留了哪些数据以及如何摆脱它。谢谢。
答案 0 :(得分:0)
约翰,别哭!我得到了解决方案。这是路径:在kv文件中,我们首先可以提到Dropdown小部件,在Drop down下,我们将提到复选框,这就是答案。.这是kv文件代码:
DropDown:
padding: 0, 0, 0, root.width * 0.4
id: dropdown
on_select: btn.text = '{}'.format(args[1])
GridLayout:
size_hint_y: None
height: 44
cols: 2
row_default_height: '10dp'
Label:
id: person
text: 'Person'
text_size: self.size
valign: 'middle'
CheckBox:
text: 'check me'
on_active:
root.on_checkbox_active(person.text, self.active)
GridLayout:
size_hint_y: None
height: 44
cols: 2
row_default_height: '10dp'
Label:
id: vehicle
text: 'Vehicle'
text_size: self.size
valign: 'middle'
CheckBox:
id: vecle
text: 'check me'
on_active:
root.on_checkbox_active(vehicle.text, self.active)
GridLayout:
size_hint_y: None
height: 44
cols: 2
row_default_height: '10dp'
Label:
id: aircraft
text: 'Air_craft'
text_size: self.size
valign: 'middle'
CheckBox:
text: 'check me'
on_active:
root.on_checkbox_active(aircraft.text, self.active)
.py文件:
class My_class(BoxLayout):
def on_checkbox_active(checkbox_ref, name, checkbox_value):
if checkbox_value:
print('', name, 'is active')
else:
print('', name, 'is inactive')
pass