
设计tkinter控制按钮实时生成函数图像出现问题
在代码中,模拟开关的行为时,出现了以下问题:
- 问题:在单击模拟开关按钮时,电压和电流从 0 时刻开始绘制,而不是从单击按钮的那一刻开始。
- 解决方法:在 update_plot 函数中,更新 current_time_index 变量,使其与当前帧号相匹配。
- 问题:按钮不能实现电路的断开和闭合。
- 解决方法:
- 在 toggle_manual_switch 函数中,更新 current_time_index 变量(在原始代码中没有更新)。
- 在 calculate_circuit_response 函数中,调整 voltageovertime 和 currentovertime 数组的赋值逻辑,以反映开关的断开和闭合状态。
修改后的代码片段:
def toggle_manual_switch(self):
# 获取当前时刻的索引
current_index = int(self.current_time_index) # FIXME: Q1.这个值没有更新过
# ... 省略其他代码 ...
def update_plot(self, frame):
# 更新 current_time_index
self.current_time_index = frame
# ... 省略其他代码 ...
def calculate_circuit_response(self, current_time_index):
# ... 省略其他代码 ...
if not self.previous_switch_state:
# 开关断开
self.VoltageOverTime[current_time_index:] = 0 # FIXME: VoltageOverTime 和 CurrentOverTime 的赋值有些问题,之前是只赋值一个点?
self.CurrentOverTime[current_time_index:] = 0
else:
# 开关闭合
self.VoltageOverTime[current_time_index:] = V_battery * np.ones_like(
self.VoltageOverTime[current_time_index:]
)
self.CurrentOverTime[current_time_index:] = V_battery / R_load * np.ones_like(
self.CurrentOverTime[current_time_index:]
)










