1

我有一个名为 _their_grid 的画布,它有许多与鼠标点击相关的相关事件。我在特定条件下禁用它 10 秒。下面是相关代码。

        print "not your turn"
        # disable opponent's grid during their turn
        self._their_grid.config(state=DISABLED)
        time.sleep(10)
        self._their_grid.config(state=NORMAL)
        print "now you can go"

然而,在十秒钟的睡眠中,我仍然设法触发了这些事件。这不应该是这种情况,因为 state=DISABLED 应该关闭所有事件。下面的代码,注释掉了 sleep,停止了所有事件。

        print "not your turn"
        # disable opponent's grid during their turn
        self._their_grid.config(state=DISABLED)
        #time.sleep(10)
        #self._their_grid.config(state=NORMAL)
        #print "now you can go"

为什么我会在 time.sleep 中出现这种奇怪的行为?

4

1 回答 1

0

time.sleep() 不会阻止事件被接受,它只是阻止它们被处理。每次您在应用程序处于睡眠状态时单击时,事件都会简单地添加到队列中,并在睡眠完成睡眠时进行处理。

您几乎应该在 GUI 中调用 sleep 。您应该做的是将状态设置为禁用,然后安排在十秒钟后重置状态:

def re_enable(self):
    self._their_grid.config(state=NORMAL)
...
self.after(10000, self.re_enable)
于 2013-03-03T00:01:15.720 回答