Kivy MDDataTable中instance_table.row_data更新异常问题
计分卡应用单元格更新问题
初始问题
开发简单计分卡应用时,在获取更新后的单元格值环节遇到以下问题:
- 通过
create_table方法创建表格时,column_sums打印的列值正常(为包含'0'的元组列表)。 - 打开弹窗更新表格数值后,界面显示已更新,但调用
update_cell函数通过instance_table.row_data访问表格数据时,仍输出包含'0'的元组列表,界面新值未同步到该数据集合中。 - 尝试复用
create_table中的逻辑重新计算column_sums,结果依旧是包含'0'的元组列表。 - 直接更新
CellRow的text属性是否错误?未找到名为value的属性。
初始代码
class Example(MDApp): dialog = None text_field = None def build(self): kv = Builder.load_string(KV) self.theme_cls.theme_style = "Light" self.sm = WindowManager() screens = [LandingWindow(name="landing"), NewGameWindow(name="new_game"), ScorecardWindow(name="scorecard"),] for screen in screens: self.sm.add_widget(screen) self.sm.current = "landing" #set to "landing" return self.sm def button_pressed(self, *args): for a in args: try: self.text_field.text += a.text except: pass def update_cell(self, instance_row, instance_table, text_field, *args): instance_row.text = text_field.text print(instance_table.row_data) def close(self): Example.get_running_app().sm.current = "landing" #TODO delete the data from the page def open_popup(self, instance_table, instance_row): box = BoxLayout(orientation="vertical") self.text_field = TextInput(size_hint_y=0.25) box.add_widget(self.text_field) grid = GridLayout(cols=3) numeric_keypad = ['7', '8', '9', '4', '5', '6', '1', '2', '3', '.', '0', 'OK'] for x in numeric_keypad: if str(x) == 'OK': ok_button = Button(text=str(x)) grid.add_widget(ok_button) else: number_button = Button(text=str(x)) number_button.bind(on_release=self.button_pressed) grid.add_widget(number_button) box.add_widget(grid) popup = Popup(title="Enter Your Score", content=box, size_hint=(0.5,0.5)) ok_button.bind(on_release=lambda *args: self.update_cell(instance_row, instance_table, self.text_field, *args), on_press=popup.dismiss) popup.open() def create_table(self): game_name = self.root.get_screen("new_game").ids['game_name'].text rounds = self.root.get_screen("new_game").ids['rounds'].value players = self.root.get_screen("new_game").ids['players'].value players = int(players) rounds = int(rounds) round_list = [("0",) * players for _ in range(rounds)] table = MDDataTable( rows_num=len(round_list), column_data=[(f"Player {x + 1}", dp(20)) for x in range(players)], row_data=round_list, on_row_press = self.open_popup ) column_sums = [sum(int(row_data[column_index]) for row_data in table.row_data) for column_index in range(players)] table.bind(on_row_press=self.open_popup) self.root.get_screen("scorecard").ids['add_table_to_scrollview'].add_widget(table) self.sm.current = "scorecard" print(column_sums) Example().run()
更新后的问题
调整代码后,表格数据(界面显示及column_sums打印的元组列表)已能同步更新,但出现新问题:代码会按固定顺序更新单元格索引,从[0](第一个单元格)开始。例如,选中第14个单元格时,实际更新的是第一个单元格;选中第5个单元格时,实际更新的是第二个单元格。
更新后的update_cell函数代码
def update_cell(self, instance_row, instance_table, text_field, *args): #instance_row.text = text_field.text # This updates the correct cell and appears on the screen but isn't actually updating the values in the list of tuples. print("row clicked is: ", instance_row) # Split the instance_row.text into individual elements row_text_elements = instance_row.text.split(',') # Find the row_index corresponding to instance_row.text row_index = -1 for index, row_data in enumerate(instance_table.row_data): if all(element in row_data for element in row_text_elements): row_index = index break if row_index != -1: # Find the cell_index corresponding to instance_row.text cell_index = -1 row_data_list = list(instance_table.row_data[row_index]) for index, element in enumerate(row_data_list): if element == instance_row.text: cell_index = index break if cell_index != -1: # Update the cell value in the row row_data_list[cell_index] = text_field.text instance_table.row_data[row_index] = tuple(row_data_list) print("row index: ", instance_table.row_data[row_index])
内容的提问来源于stack exchange,提问作者Callum
相关产品推荐
相关产品推荐

