Python问题:While循环中数组键值对被意外覆盖(键名不同)
进度条功能数据覆盖问题
需求说明
我正在开发一个进度条功能,可通过更新Excel中的数值进行控制,并在.svg文件中实时显示变化。
问题描述
我有一个可重复执行的循环,循环中字典内的键值对名称会在两个选项间切换。但即使我将这两个选项分开处理,且打印输出显示值正确,存储的数据仍会被第一次进入方法时的键值对覆盖。
相关代码片段
字典声明(类属性)
class RangeBar(GraphicsPort): _opacity = { "firstPhase": 1, "thirdPhase": 1 }
main()中的循环逻辑
if (rb._empty["firstPhase"] == False): print("Start Phase 1 Loop") rb.partialPhase(False, "firstPhase") print("Exit Phase 1 Loop") if (rb._empty["fillPhase"] == False): print("Start Phase 2 Loop") rb.fillPhase(False, "fillPhase") print("Exit Phase 2 Loop") if (rb._empty["thirdPhase"] == False): print("Start Phase 3 Loop") rb.partialPhase(False, "thirdPhase") print("Exit Phase 3 Loop")
出现问题的partialPhase方法
def partialPhase(self, baseCellModified, phase): if (baseCellModified != False): self.setValue(baseCellModified) self._opacity[phase] = baseCellModified / 100 self.sheet_action("update_sheet_data",cell="C3",number=100) if (phase == "firstPhase"): self.setValue(86.9) if (phase == "thirdPhase"): self.setValue(12.9) print("New opacity for 'phase':",phase,"from: 'self._opacity[phase]':", self._opacity[phase]) self.sheet_action("opacity_update", opacity=str(self._opacity[phase])) self.sheet_action("update_sheet_data",cell="D3",number=self.getValue()) svg = str(self.__utils) text_file = open("./1.svg", "w") text_file.write(svg) text_file.close() timeToAction = self.sheet_action("get_sheet_data", type="numb", cell="F3") * 60 time.sleep(timeToAction) while self._opacity[phase] > 0: timeToAction = self.sheet_action("get_sheet_data", type="numb", cell="F3") * 60 changeCell = self.sheet_action("get_sheet_data", type="numb", cell="E3") action = self.identifyAction("G3") if (action == "decrement"): self._opacity[phase] -= (changeCell / 8) print("'phase' is showing opacity of:", phase) print("'self._opacity[phase]' is showing:", self._opacity[phase]) if (action == "increment"): self._opacity[phase] += (changeCell / 8) if self._opacity[phase] < 0: print("Entering opacity minor to 0") print("'phase' is showing opacity of:", phase) print("'self._opacity[phase]' is showing:", self._opacity[phase]) self._opacity[phase] = 0 self._empty[phase] = True if (phase == "thirdPhase"): self._critical = True self.sheet_action("opacity_update", opacity=str(self._opacity[phase])) self.sheet_action("update_sheet_data",cell="D3",number=self.getValue()) svg = str(self.__utils) text_file = open("./1.svg", "w") text_file.write(str(self.__utils)) text_file.close() time.sleep(timeToAction)
问题现象
完成第一阶段(firstPhase)并将其透明度设为0后,进入第三阶段(thirdPhase)时,打印显示当前处理的是thirdPhase,但实际操作的却是firstPhase的键值,导致firstPhase的透明度被重置为1,且后续始终操作firstPhase的键值,直到其再次变为0。
相关现象截图
- 初始.svg效果:

- 第一阶段打印输出:

- 第一阶段设为0后:

- 完成第二阶段后:

- 问题出现时:

- 第三阶段却操作firstPhase:

- 直到firstPhase再次变为0:

内容的提问来源于stack exchange,提问作者Last
相关产品推荐
相关产品推荐

