[Python] Multiple screens | python custom Tkinter

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    In my Tkinter chatbot app, clicking the 'History' menu opens a new window every time. How can I make it so only one history window exists, and if it's already open, bring it to focus instead of opening another?

    my history window code:

    def show_history():
    win.withdraw()

    def open_file(file_path, text_widget):
    try:
    with open(file_path, "r", encoding="utf-8") as file:
    content = file.read()
    text_widget.delete("1.0", "end")
    text_widget.insert("1.0", content)
    except FileNotFoundError:
    text_widget.delete("1.0", "end")
    text_widget.insert("1.0", f"Error: File not found at {file_path}")
    except Exception as e:
    text_widget.delete("1.0", "end")
    text_widget.insert("1.0", f"An error occurred: {e}")

    def close_new_window():
    root.destroy()
    win.deiconify()

    root = ctk.CTkToplevel()
    root.geometry(f"{HEIGHT}x{WIDTH}")

    file_content_textbox = ctk.CTkTextbox(root, wrap="word")
    file_content_textbox.pack(fill="both", expand=True, padx=10, pady=10)

    file_to_load = resource_path("data.txt")

    open_file(file_to_load, file_content_textbox)

    button = ctk.CTkButton(root, text="Close", command=close_new_window)
    button.pack(pady=50)

    root.mainloop()

    Continue reading...

Compartilhe esta Página