[Python] Trying to get tkinter to save info from a second window. Text box information is...

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

  1. Stack

    Stack Membro Participativo

    My code consist of a page that has a simple button, you click the button and it brings a popup to store information. You have title and desc fields etc. but only the text fields seem to be saving (as variable). I have a checkbox input on that page, and whether I selected it or not, it returns the same value of false for all of the checkboxes. Here is my code, can anyone please tell me what I'm doing wrong

    def add_page(button_ref):
    page_window = tk.Toplevel(root)
    page_window.title("Add Page")

    main_x = root.winfo_rootx() # Left edge of main window
    main_y = root.winfo_rooty() # Top edge of main window

    # --- Button coordinates ---
    btn_y = button_ref.winfo_rooty()
    btn_height = button_ref.winfo_height()

    # --- Popup size ---
    popup_width = int(root.winfo_width() * 0.5)
    popup_height = int(root.winfo_height() * 0.5)

    # --- Position popup below button, aligned to main window left ---
    popup_x = main_x
    popup_y = btn_y + btn_height + 5

    page_window.geometry(f"{popup_width}x{popup_height}+{popup_x}+{popup_y}")

    page_window.minsize(int(screen_width * 0.9), int(screen_height * 0.5))

    page_vars = {
    "theme": tk.StringVar(value="default"),
    "navbar": tk.BooleanVar(value=False),
    "sidebar": tk.BooleanVar(value=False),
    "center_text": tk.BooleanVar(value=False)
    }

    # --- Page title ---
    tk.Label(page_window, text="Page Title:").grid(row=0, column=0, sticky="w")
    title_entry = tk.Entry(page_window, width=40)
    title_entry.grid(row=0, column=1)
    add_placeholder(title_entry, "e.g. Main Page")

    # --- Filename ---
    tk.Label(page_window, text="Filename:").grid(row=1, column=0, sticky="w")
    filename_entry = tk.Entry(page_window, width=40)
    filename_entry.grid(row=1, column=1)
    add_placeholder(filename_entry, "e.g. MainPage.html (must include .html)")

    # --- Description ---
    tk.Label(page_window, text="Description:").grid(row=2, column=0, sticky="w")
    desc_entry = tk.Entry(page_window, width=40)
    desc_entry.grid(row=2, column=1)
    add_placeholder(desc_entry, "e.g. This is the pages deseciprion")

    # --- Theme Dropdown ---
    tk.Label(page_window, text="Theme:").grid(row=3, column=0, sticky="w")
    theme_var = tk.StringVar(value="default")
    theme_menu = ttk.Combobox(page_window, textvariable=theme_var, values=list(themes.keys()))
    theme_menu.grid(row=3, column=1)

    # --- Fixed navbar ---
    navbar_var = tk.BooleanVar(value=False)
    tk.Checkbutton(page_window, text="Fixed Navbar", variable=page_vars["navbar"]).grid(row=4, column=0, columnspan=2, sticky="w")

    # --- Sidebar ---
    sidebar_var = tk.BooleanVar(value=False)
    tk.Checkbutton(page_window, text="Sidebar", variable=page_vars["sidebar"]).grid(row=5, column=0, columnspan=2, sticky="w")

    # --- Center Text ---
    center_text = tk.BooleanVar(master=page_window, value=False)
    tk.Checkbutton(page_window, text="Center Text", variable=center_text, onvalue=True, offvalue=False).grid(row=6, column=0, columnspan=2, sticky="w")

    # --- Save Button ---
    def save_page():
    page = {
    "title": title_entry.get(),
    "filename": filename_entry.get(),
    "description": desc_entry.get(),
    "layout": {
    "theme": theme_var.get(),
    "navbar": page_vars["navbar"].get(),
    "sidebar": page_vars["sidebar"].get(),
    "center_text": page_vars["center_text"].get()
    },
    "content_blocks": []
    }
    messagebox.showinfo(
    "DEBUG",
    f"Navbar={page_vars['navbar'].get()}, Sidebar={page_vars['navbar'].get()}, Center={page_vars['center_text'].get()}"
    )
    pages.append(page)
    generate_page(page, pages, themes, header_footer_presets)

    page_window.destroy()
    messagebox.showinfo("Saved", f"Page '{page['title']}' saved as HTML")

    tk.Button(page_window, text="Save Page", command=save_page).grid(row=7, column=0, columnspan=2, pady=10)


    I understand the code is messy and some things don't line up correctly, but that's because I've been doing trial and error for so long trying to fix this. Any help is appreciated, also I am running a debug program but it still says all value return as false Here is a runnable version of the code (I'm pretty sure):

    import os
    import json
    import zipfile
    import requests
    # --- GUI Imports ---
    import tkinter as tk
    from tkinter import ttk, messagebox, filedialog

    themes = {
    "default": {
    "background_color": "white",
    "text_color": "black",
    "button_bg": "#eee",
    "button_text": "black",
    "navbar_bg": "#333",
    "sidebar_bg": "#222",
    "sidebar_text": "#fff",
    "collapsible_bg": "#eee",
    "collapsible_content_bg": "#f9f9f9"
    },
    "dark": {
    "background_color": "#121212",
    "text_color": "#eee",
    "button_bg": "#333",
    "button_text": "white",
    "navbar_bg": "#333",
    "sidebar_bg": "#222",
    "sidebar_text": "#eee",
    "collapsible_bg": "#333",
    "collapsible_content_bg": "#1e1e1e"
    },
    "ocean": {
    "background_color": "#e0f7fa",
    "text_color": "#004d40",
    "button_bg": "#00796b",
    "button_text": "white",
    "navbar_bg": "#004d40",
    "sidebar_bg": "#00695c",
    "sidebar_text": "#e0f7fa",
    "collapsible_bg": "#b2dfdb",
    "collapsible_content_bg": "#e0f2f1"
    }
    }

    # --- Main Window ---
    root = tk.Tk()

    # Get screen size
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # Set window to 80% of screen size
    width = int(screen_width * 0.8)
    height = int(screen_height * 0.8)
    root.geometry(f"{width}x{height}")

    # Add Page button
    add_page_btn = tk.Button(root, text="Add Page", command=lambda: add_page(add_page_btn))
    add_page_btn.pack(pady=5)

    #placeholders
    def add_placeholder(entry, placeholder):
    entry.insert(0, placeholder)
    entry.config(fg='gray')

    def on_focus_in(event):
    if entry.get() == placeholder:
    entry.delete(0, tk.END)
    entry.config(fg='black')

    def on_focus_out(event):
    if entry.get() == "":
    entry.insert(0, placeholder)
    entry.config(fg='gray')

    entry.bind("<FocusIn>", on_focus_in)
    entry.bind("<FocusOut>", on_focus_out)

    # --- Modified add_page to anchor below button ---
    def add_page(button_ref):
    """Open a new window to enter page information, positioned below the button."""
    page_window = tk.Toplevel(root)
    page_window.title("Add Page")

    main_x = root.winfo_rootx() # Left edge of main window
    main_y = root.winfo_rooty() # Top edge of main window

    # --- Button coordinates ---
    btn_y = button_ref.winfo_rooty()
    btn_height = button_ref.winfo_height()

    # --- Popup size ---
    popup_width = int(root.winfo_width() * 0.5)
    popup_height = int(root.winfo_height() * 0.5)

    # --- Position popup below button, aligned to main window left ---
    popup_x = main_x
    popup_y = btn_y + btn_height + 5

    page_window.geometry(f"{popup_width}x{popup_height}+{popup_x}+{popup_y}")

    page_window.minsize(int(screen_width * 0.9), int(screen_height * 0.5))

    page_vars = {
    "theme": tk.StringVar(value="default"),
    "navbar": tk.BooleanVar(value=False),
    "sidebar": tk.BooleanVar(value=False),
    "center_text": tk.BooleanVar(value=False)
    }

    # --- Page title ---
    tk.Label(page_window, text="Page Title:").grid(row=0, column=0, sticky="w")
    title_entry = tk.Entry(page_window, width=40)
    title_entry.grid(row=0, column=1)
    add_placeholder(title_entry, "e.g. Main Page")

    # --- Filename ---
    tk.Label(page_window, text="Filename:").grid(row=1, column=0, sticky="w")
    filename_entry = tk.Entry(page_window, width=40)
    filename_entry.grid(row=1, column=1)
    add_placeholder(filename_entry, "e.g. MainPage.html (must include .html)")

    # --- Description ---
    tk.Label(page_window, text="Description:").grid(row=2, column=0, sticky="w")
    desc_entry = tk.Entry(page_window, width=40)
    desc_entry.grid(row=2, column=1)
    add_placeholder(desc_entry, "e.g. This is the pages deseciprion")

    # --- Theme Dropdown ---
    tk.Label(page_window, text="Theme:").grid(row=3, column=0, sticky="w")
    theme_var = tk.StringVar(value="default")
    theme_menu = ttk.Combobox(page_window, textvariable=theme_var, values=list(themes.keys()))
    theme_menu.grid(row=3, column=1)

    # --- Fixed navbar ---
    navbar_var = tk.BooleanVar(value=False)
    tk.Checkbutton(page_window, text="Fixed Navbar", variable=page_vars["navbar"]).grid(row=4, column=0, columnspan=2, sticky="w")

    # --- Sidebar ---
    sidebar_var = tk.BooleanVar(value=False)
    tk.Checkbutton(page_window, text="Sidebar", variable=page_vars["sidebar"]).grid(row=5, column=0, columnspan=2, sticky="w")

    # --- Center Text ---
    center_text = tk.BooleanVar(master=page_window, value=False)
    tk.Checkbutton(page_window, text="Center Text", variable=center_text, onvalue=True, offvalue=False).grid(row=6, column=0, columnspan=2, sticky="w")

    # --- Save Button ---
    def save_page():
    page = {
    "title": title_entry.get(),
    "filename": filename_entry.get(),
    "description": desc_entry.get(),
    "layout": {
    "theme": theme_var.get(),
    "navbar": page_vars["navbar"].get(),
    "sidebar": page_vars["sidebar"].get(),
    "center_text": page_vars["center_text"].get()
    },
    "content_blocks": []
    }
    """
    # --- Debug Popup ---
    debug_window = tk.Toplevel(root)
    debug_window.title("DEBUG: Page Layout")
    debug_text = tk.Text(debug_window, width=50, height=10)
    debug_text.pack(padx=10, pady=10)
    debug_text.insert(tk.END, f"Page title: {page['title']}\n")
    debug_text.insert(tk.END, f"Filename: {page['filename']}\n")
    debug_text.insert(tk.END, f"Theme: {page['layout']['theme']}\n")
    debug_text.insert(tk.END, f"Navbar: {page['layout']['navbar']}\n")
    debug_text.insert(tk.END, f"Sidebar: {page['layout']['sidebar']}\n")
    debug_text.insert(tk.END, f"Center Text: {page['layout']['center_text']}\n")
    debug_text.config(state=tk.DISABLED)
    """
    pages.append(page)
    generate_page(page, pages, themes, header_footer_presets)

    page_window.destroy()
    messagebox.showinfo("Saved", f"Page '{page['title']}' saved as HTML")

    tk.Button(page_window, text="Save Page", command=save_page).grid(row=7, column=0, columnspan=2, pady=10)

    if __name__ == "__main__":
    pages = []
    root = tk.Tk()
    root.title("Site")

    root.mainloop()

    Continue reading...

Compartilhe esta Página