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

如何让Tkinter中两个GUI窗口及函数按顺序执行?

Fixing Sequential Tkinter Window Execution

I see the issue—your code is spawning multiple Tk instances and running their event loops simultaneously, which breaks the sequential flow you want. Let's fix this by restructuring the code to use a single main window and trigger the text editor only after the user selects a file.

Key Issues in Your Original Code

  • Multiple Tk() instances: Tkinter only supports one main application window; additional windows should use Toplevel().
  • The text editor window initializes immediately, before the user interacts with the browse window.
  • Duplicate mainloop() calls, which cause unexpected behavior.

Corrected Code

from tkinter import *
from tkinter import filedialog

def open_text_editor(file_path):
    # Create a new top-level window for the text editor
    editor_window = Toplevel()
    editor_window.title("Text Editor")

    # Load file content into the text widget
    T = Text(editor_window, state='normal', height=20, width=70)
    T.pack()
    with open(file_path, 'r') as f:
        T.insert(END, f.read())

    # Save function for the editor
    def save_file():
        content = T.get('1.0', END)
        with open(file_path, 'wt') as f:
            f.write(content)
        editor_window.destroy()  # Optional: close editor after save

    # Add save button
    save_btn = Button(editor_window, text='Save', command=save_file)
    save_btn.pack(side=RIGHT, padx=55, pady=10)

def browse_files():
    # Open file dialog to select a text file
    file_path = filedialog.askopenfilename(filetypes=[("Text file", "*.txt")])
    if file_path:  # Only proceed if a file was selected
        main_window.withdraw()  # Hide the browse window
        open_text_editor(file_path)  # Launch the text editor

# Main application window (only one Tk instance!)
main_window = Tk()
main_window.title('Edit and save text files by Ali')

# Browse button frame
frame = Frame(main_window)
browse_btn = Button(frame, text='Browse', command=browse_files)
browse_btn.pack(side=RIGHT, padx=55)
frame.pack(padx=100, pady=30)

# Start the main event loop (only one needed!)
main_window.mainloop()

How This Works

  1. Main Window: The browse window appears first, waiting for the user to click "Browse".
  2. File Selection: When you select a .txt file, the browse window hides, and the text editor window opens with the file's content loaded.
  3. Edit & Save: You can modify the text, then click "Save" to write changes back to the selected file (the editor window closes after saving, but you can remove that line if you want it to stay open).

This structure ensures the sequential flow you want while following Tkinter's best practices.

Content of the question originates from Stack Exchange, question author Epiphnac.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 10:53:11