Tkinter界面实时绘制函数图像:如何实现按钮控制电路的断开与闭合并从点击时刻开始绘制?

花韻仙語
发布: 2024-11-19 13:40:47
原创
624人浏览过

Tkinter界面实时绘制函数图像:如何实现按钮控制电路的断开与闭合并从点击时刻开始绘制?

设计tkinter控制按钮,实时生成函数图像

问题:使用tkinter设计了界面,但点击按钮后,函数图像从0开始,而不是按钮点击时刻开始,无法实现电路的断开和闭合。

解决方案:

修改代码中的关键部分:

def toggle_manual_switch(self):
    # 获取当前时刻的索引
    current_index = int(self.current_time_index)

    # 进行状态切换
    self.simulator.switch_states[current_index] = not self.simulator.switch_states[current_index]

    # 更新按钮文本和命令
    if self.manual_switch_button["text"] == "Close Circuit":
        self.manual_switch_button["text"] = "Open Circuit"
    else:
        self.manual_switch_button["text"] = "Close Circuit"

    # 更新整个图表,传递当前时间点的索引
    self.update_plot(current_index)
    self.canvas.draw_idle()


def calculate_circuit_response(self, current_time_index):
    # 检查当前时间点是否有开关切换发生
    if current_time_index > self.previous_switch_time_index:
        # 检查当前时间点的开关状态是否与前一时刻不同
        if self.switch_states[current_time_index] != self.switch_states[current_time_index - 1]:
            self.previous_switch_state = not self.previous_switch_state
            next_switch_index = current_time_index + np.argmax(
                self.switch_states[current_time_index:] != self.switch_states[current_time_index])
            if not self.previous_switch_state:
                # 开关断开
                self.VoltageOverTime[current_time_index:] = 0
                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:])
            # 更新上一次开关切换的时间索引
            self.previous_switch_time_index = next_switch_index

def update_plot(self, frame):
    self.simulator.calculate_circuit_response(frame)
    time = t[frame]
    # 更新时间索引
    self.current_time_index = frame

    V_circuit = self.simulator.VoltageOverTime[:frame + 1]
    I_circuit = self.simulator.CurrentOverTime[:frame + 1]

    self.V_line.set_data(t[:len(V_circuit)], V_circuit)
    self.I_line.set_data(t[:len(I_circuit)], I_circuit)
    self.axs[0].set_xlim(0, t_max)
    self.axs[1].set_xlim(0, t_max)
    self.axs[0].set_ylim(0, 20)
    self.axs[1].set_ylim(0, 2)
    print("Plot updated")  # 添加这行代码来确认图表是否被更新
    print("Plot Voltage:", V_circuit[-1], "V")
    return self.V_line, self.I_line
登录后复制

特点:

  • 修改了update_plot函数,更新了时间索引current_time_index。
  • 修改了calculate_circuit_response函数,即使仅发生一次状态切换,也能更新电压和电流值。
  • 修改了toggle_manual_switch函数,点击按钮后直接进行状态切换。

这些修改后,电路可以从按钮点击时刻开始绘制,并且按钮操作能够实现电路的断开和闭合。

以上就是Tkinter界面实时绘制函数图像:如何实现按钮控制电路的断开与闭合并从点击时刻开始绘制?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号