python - 未处理按钮事件
问题描述
我正在将几年前在 Perl 中编写的模拟移植到 Python,当我单击 STOP 按钮时,我遇到了停止主模拟循环的问题。
它是如何工作的:有一个我可以绘制的画布以及一个 RUN 和一个 STOP 按钮。RUN 按钮启动模拟,STOP 按钮停止模拟。单击 STOP 按钮时,该stop()
函数将全局标志设置Run_sim
为 0,并在主模拟循环中检查该标志while Run_sim != STOP:
。
我知道在模拟未运行时stop()
正在调用该函数并设置标志,因为我在添加模拟循环代码之前对其进行了测试。Run_sim
但是当模拟循环运行时,它会在单击 STOP 按钮时出现,该事件未被处理,并且stop()
直到我Ctrl-C从终端调用该函数。
关于如何解决这个问题的任何建议?
我删除了不适用于此的代码。
import sys
from Tkinter import *
import math
import time
import tkMessageBox
def stop():
global Run_sim
Run_sim = STOP
###############
# main part of simulation
###############
def display_cluster():
while Run_sim != STOP:
.
.
.
bunch of code here
.
.
.
# end while Run_sim
# end display_cluster()
stop_button = Button(button_frame, text="Stop", fg="red", command = stop)
stop_button.pack(side = 'left')
解决方案
推荐阅读
- javascript - 设置 ES6 默认参数来处理空字符串
- python - Sklearn SVM - 如何获取错误预测列表?
- php - 安装失败:无法在 ubuntu 上的 wordpress nginx 中创建目录
- anaconda - 运行“conda install conda”后,yum 和 dnf 被破坏
- c# - 将项目插入可跟踪集合时,CollectionChanged 未触发
- postgresql - pg_restore 旧数据库备份
- acumatica - 服务订单行注释未在发票上打印
- c# - 除非单击,否则如何防止文本框获得焦点?
- apache-spark - 使用--properties添加键时Dataproc添加额外字段?
- javascript - 如何使用 2 个数组遍历对象?