You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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效果:初始SVG进度条
  • 第一阶段打印输出:第一阶段打印
  • 第一阶段设为0后:第一阶段透明度为0
  • 完成第二阶段后:第二阶段完成后
  • 问题出现时:问题出现时的SVG
  • 第三阶段却操作firstPhase:第三阶段打印异常
  • 直到firstPhase再次变为0:firstPhase再次为0

内容的提问来源于stack exchange,提问作者Last

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 15:15:44