翼度科技»论坛 编程开发 python 查看内容

【日常收支账本】【Day04】优化编辑动账记录的操作——QTableWidget单元格

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
一、项目地址

https://github.com/LinFeng-BingYi/DailyAccountBook
二、新增

1. 在表格中设置选项列表,让用户更快地编辑动账记录

1.1 功能详述

为表格中以下字段设置选项列表:
1. 需求强度(由"基本需求"更名)
温饱:基本维持生存且不铺张浪费的消费行为
小康:在温饱的基础上,可以使生活变得比较舒适的消费行为
奢华:可有可无的,或超出自身消费水平的消费行为

该属性意在于让用户更好地明白基本消费与超额消费占比,以便遏制不必要的消费行为
2. 类别(支出表、收入表)

计划在将来支持用户自定义支出或收入类别。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用
3. 支出账户、收入账户、关联账户

计划在将来支持用户自定义账户列表。目前写死在代码中,可以在CommonFiles/ConstArgs.py文件中修改后使用
1.2 代码实现

思路分析

支出、收入、转移表格中的列名有大量重复,因此将所有列的初始化集中到一个函数中,根据列名执行对应初始化方式。同时,还要区分已存在记录的行和空白行。综上,实现了三个函数:
函数名作用setExistTableCell为已存在记录的表格行设置单元格setBlankTableCell为表格的空白行设置单元格getExistTableCell获取某行记录的数据,组装成字典代码
  1.     def setExistTableCell(self, tableWidget, current_row, value_dict: dict, const_class):
  2.         """
  3.         Describe: 为已存在记录的表格行设置单元格。根据列名设置对应类型(格式)的单元格
  4.         Args:
  5.             tableWidget: QTableWidget
  6.                 涉及的表格
  7.             current_row: int
  8.                 本条记录所在行号
  9.             value_dict: dict
  10.                 记录字典
  11.             const_class: Union[ExpenseConst, IncomeConst, MovementConst]
  12.                 记录类型对应的常量类
  13.         """
  14.         keys_list = list(value_dict.keys())
  15.         for key, value in value_dict.items():
  16.             if key == 'necessity':
  17.                 comboBox = ComboBoxInTableWidget(NECESSITY, value)
  18.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  19.             elif key == 'value':
  20.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(str(value)))
  21.             elif key == 'category':
  22.                 comboBox = ComboBoxInTableWidget(const_class.CATEGORY, value)
  23.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  24.             elif key == 'detail':
  25.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(value))
  26.             elif key == 'describe':
  27.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(value))
  28.             elif key == 'from':
  29.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
  30.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  31.             elif key == 'to':
  32.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
  33.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  34.             elif key == 'associatedFund':
  35.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, value)
  36.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  37.             else:
  38.                 print("未知的记录属性!!")
  39.                 return
  40.         tableWidget.setCellWidget(current_row, len(keys_list), self.buttonsForExistRow(tableWidget))
  41.     def setBlankTableCell(self, tableWidget, current_row, const_class):
  42.         """
  43.         Describe: 为表格的空白行设置单元格。根据列名设置对应类型(格式)的单元格
  44.         Args:
  45.             tableWidget: QTableWidget
  46.                 涉及的表格
  47.             current_row: int
  48.                 本条记录所在行号
  49.             const_class: Union[ExpenseConst, IncomeConst, MovementConst]
  50.                 记录类型对应的常量类
  51.         """
  52.         keys_list = [const_class.TABLEWIDGET_COLUMN_HEAD[tableWidget.horizontalHeaderItem(i).text()] for i in range(tableWidget.columnCount()-1)]
  53.         for key in keys_list:
  54.             if key == 'necessity':
  55.                 comboBox = ComboBoxInTableWidget(NECESSITY, 'True')
  56.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  57.             elif key == 'value':
  58.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(''))
  59.             elif key == 'category':
  60.                 comboBox = ComboBoxInTableWidget(const_class.CATEGORY, 0)
  61.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  62.             elif key == 'detail':
  63.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(''))
  64.             elif key == 'describe':
  65.                 tableWidget.setItem(current_row, keys_list.index(key), QTableWidgetItem(' '))
  66.             elif key == 'from':
  67.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, 0)
  68.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  69.             elif key == 'to':
  70.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, 0)
  71.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  72.             elif key == 'associatedFund':
  73.                 comboBox = ComboBoxInTableWidget(fundConst.CATEGORY, None)
  74.                 tableWidget.setCellWidget(current_row, keys_list.index(key), comboBox)
  75.             else:
  76.                 print("未知的记录属性!!: {}", format(key))
  77.                 return
  78.         tableWidget.setCellWidget(current_row, len(keys_list), self.buttonsForNewRow(tableWidget))
  79.     def getExistTableCell(self, tableWidget, current_row, const_class):
  80.         new_data_dict = OrderedDict()
  81.         for i in range(tableWidget.columnCount() - 1):
  82.             key = const_class.TABLEWIDGET_COLUMN_HEAD[tableWidget.horizontalHeaderItem(i).text()]
  83.             if key == 'necessity':
  84.                 comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
  85.                 new_data_dict[key] = str(comboBox.getKeyByCurrentText())
  86.             elif key == 'value':
  87.                 new_data_dict[key] = tableWidget.item(current_row, i).text()
  88.             elif key == 'category':
  89.                 comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
  90.                 new_data_dict[key] = str(comboBox.getKeyByCurrentText())
  91.             elif key == 'detail':
  92.                 new_data_dict[key] = tableWidget.item(current_row, i).text()
  93.             elif key == 'describe':
  94.                 new_data_dict[key] = tableWidget.item(current_row, i).text()
  95.             elif key == 'from':
  96.                 comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
  97.                 new_data_dict[key] = str(comboBox.getKeyByCurrentText())
  98.             elif key == 'to':
  99.                 comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
  100.                 new_data_dict[key] = str(comboBox.getKeyByCurrentText())
  101.             elif key == 'associatedFund':
  102.                 comboBox: ComboBoxInTableWidget = tableWidget.cellWidget(current_row, i)
  103.                 new_data_dict[key] = str(comboBox.getKeyByCurrentText())
  104.             else:
  105.                 print("未知的记录属性!!: {}", format(key))
  106.                 return None
  107.         return new_data_dict
复制代码
来源:https://www.cnblogs.com/LinfengBingyi/p/17747789.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具