Python3.6下Tkinter打开新窗口导致程序冻结问题排查
Hey there! Let's break down why your second window is freezing up—this is a common pitfall when switching between GUI windows in Python, especially with libraries like Tkinter. The core issue almost always ties back to how you're handling the first window's event loop and launching the second one. Here's what to check:
1. You didn't fully terminate the first GUI's main loop
Calling destroy() on your first window closes the UI, but it doesn't always stop the underlying mainloop() from running. If that loop is still blocking the main thread, your second window's event loop can't take over, leading to a frozen, unresponsive window (no title bar controls, just the icon).
Fix this by pairing destroy() with quit() to fully shut down the first loop before launching the second:
# Example exit logic in first.py def switch_to_second_window(): # Destroy the first window first_root.destroy() # Terminate the main loop completely first_root.quit() # Now launch the second window import second second.start_second_window()
If you're launching the second script via subprocess or os.system, make sure the first script's process fully exits before spawning the second—otherwise, you might hit resource locks.
2. You're trying to run multiple event loops in the same thread
Nearly all Python GUI libraries (Tkinter, PyQt, etc.) use a single-threaded event loop model. If you start the second window's mainloop() while the first is still running (even if the window is destroyed), the second loop can't get control of the main thread. This is the most likely cause of your frozen window.
The fix here is strict sequential execution:
- Fully shut down the first window's event loop (using the
destroy()+quit()combo above) - Only then initialize and run the second window's event loop
Avoid trying to run both loops in the same thread—this will always cause blocking issues.
3. Your second window's initialization has a blocking issue
Double-check your second.py code for any blocking operations that might be freezing the UI before it can render properly. For example:
- A long-running task (like a file read, network call) in the main thread before calling
mainloop() - Forgetting to actually start the second window's event loop with
mainloop() - Missing critical initialization steps (like setting the window title, which might cause the title bar to fail rendering)
Here's a safe template for second.py to rule out these issues:
import tkinter as tk def start_second_window(): second_root = tk.Tk() second_root.title("Second Window") # Ensures title bar renders # Add your UI components here second_root.mainloop() # Don't forget this! if __name__ == "__main__": start_second_window()
4. Unreleased resources from the first window
In rare cases, leftover resources from the first window (like global variables, open file handles, or unclosed connections) can interfere with the second window's ability to access system resources. Try adding cleanup steps before launching the second window:
- Clear any global variables tied to the first window
- Close any open files/network connections
- Explicitly delete references to the first window object with
del first_root
Quick Troubleshooting Step
To confirm if the first main loop is the problem, add a print("First loop terminated") right after first_root.quit()—if you don't see this message in your console, the first loop is still running and blocking everything else.
内容的提问来源于stack exchange,提问作者R. Blackheart




