首页 > 解决方案 > 未处理按钮事件

问题描述

我正在将几年前在 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')

标签: pythonpython-2.7tkinter

解决方案


推荐阅读