You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter中临时暂停GUI?after、time.sleep方案无效

Troubleshooting Your Simon Game's Sequence Display (Bypassing time.sleep()/after() Issues)

Hey there! I totally get it—you’ve already tried the go-to fixes like time.sleep() and Tkinter’s after() for your Simon game, but they’re not working as expected. That’s super frustrating, especially when you know the problem’s been discussed before but none of the standard solutions fit your case. Let’s break this down.

First, let’s look at your code snippet (I notice it cuts off, but we can work with what’s here):

from tkinter import *
import time,random

root = Tk()

class Simon:
    def __init__(self):
        print('SIMON')
        self.red = PhotoImage(file="Red.gif")
        self.blue = PhotoImage(file="Blue.gif")
        self.green = PhotoImage(file = "Green.gif")
        self.yellow = PhotoImage(file= "Yellow.gif")
        self.white = PhotoImage(file= "Blankimage.gif")
        self.randlist=[]
        self.storeran...  # Your code cuts off here!

Why time.sleep() and Basic after() Fail

Tkinter runs on a single event loop—time.sleep() blocks this loop entirely, freezing your UI so none of your color changes show up until the sleep is done. A naive after() call might fire all your color updates at once (or out of order) because it’s asynchronous, which breaks the sequential "light up, wait, turn off, repeat" flow Simon needs.

A Reliable Recursive after() Approach

The fix is to use recursive after() calls to step through your sequence one at a time, keeping everything in sync with the event loop. Here’s how to implement this in your Simon class:

First, add some missing variables to your __init__ (assuming you have button widgets set up):

def __init__(self):
    print('SIMON')
    # ... your existing image setup ...
    self.randlist = []
    # Initialize buttons (adjust to match your UI setup)
    self.red_button = Button(root, image=self.white)
    self.red_button.pack()
    self.blue_button = Button(root, image=self.white)
    self.blue_button.pack()
    self.green_button = Button(root, image=self.white)
    self.green_button.pack()
    self.yellow_button = Button(root, image=self.white)
    self.yellow_button.pack()
    # Track where we are in the sequence
    self.current_step = 0

Then add two methods to handle playing the sequence:

def play_sequence(self):
    # If we haven't finished the sequence yet
    if self.current_step < len(self.randlist):
        current_color = self.randlist[self.current_step]
        # Light up the corresponding button
        self.light_up_color(current_color)
        # Schedule turning off the color after 1 second
        root.after(1000, self.turn_off_color, current_color)
        # Move to the next step in the sequence
        self.current_step += 1

def light_up_color(self, color):
    if color == "red":
        self.red_button.config(image=self.red)
    elif color == "blue":
        self.blue_button.config(image=self.blue)
    elif color == "green":
        self.green_button.config(image=self.green)
    elif color == "yellow":
        self.yellow_button.config(image=self.yellow)

def turn_off_color(self, color):
    # Reset the button to white
    if color == "red":
        self.red_button.config(image=self.white)
    elif color == "blue":
        self.blue_button.config(image=self.white)
    elif color == "green":
        self.green_button.config(image=self.white)
    elif color == "yellow":
        self.yellow_button.config(image=self.white)
    # Wait 500ms before starting the next step
    root.after(500, self.play_sequence)

How This Works

  • play_sequence() checks if we’re still in the sequence, lights up the current color, then schedules turning it off after 1 second.
  • turn_off_color() resets the button and schedules the next step in the sequence after a short delay (500ms here—adjust to your preference).
  • Since each step is scheduled via after(), the Tkinter event loop never blocks, so your UI stays responsive and colors update in order.

Quick Tips

  • Make sure self.randlist stores strings like "red", "blue", etc., that match the checks in light_up_color() and turn_off_color().
  • If your buttons are named differently (e.g., self.btn_red instead of self.red_button), adjust the code to match your widget names.
  • Test different delay values (the 1000 and 500 in after()) to get the timing right for your game.

内容的提问来源于stack exchange,提问作者andrewJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:13:54