tkinterframescustomtkinter

when switching frames in custom tkinter the entries are empty while returning to the previous frame


So i was coding an app using custom tkinter i followed a youtube tutorial for option_frame and main_frame so its very confusing i mean when i use the basic_page select the files and after i gone to icon_page and selected the icon and when come back to basic_page its entries are empty and when i gone to icon_page back it is also empty how it works can u guys help me

def basic_page():
    global input_file_entry, output_file_entry

    # Create StringVar instances for input and output file paths
    input_file_var = StringVar()
    output_file_var = StringVar()

    def choose_input_file():
        input_file = filedialog.askopenfilename(title="Select Input File")
        input_file_var.set(input_file)  # Set the selected file in the StringVar

    def choose_output_file():
        output_file = filedialog.asksaveasfilename(title="Name anything...",)
        output_file_var.set(output_file)  # Set the selected file in the StringVar

    basic_page_frame = CTkFrame(main_frame)

    input_file_entry = CTkEntry(basic_page_frame, textvariable=input_file_var, placeholder_text="write Path to the file u want to encrypt manually", width=450)
    input_file_entry.pack(pady=50)

    input_file_button = CTkButton(basic_page_frame, text="Choose Input file", command=choose_input_file, width=150, corner_radius=40)
    input_file_button.pack()

    output_file_entry = CTkEntry(basic_page_frame, textvariable=output_file_var, placeholder_text="write Path to the output file manually donot add .exe", width=450)
    output_file_entry.pack(pady=50)

    output_file_button = CTkButton(basic_page_frame, text="Save as", command=choose_output_file, width=150, corner_radius=40)
    output_file_button.pack()

    basic_page_frame.pack(fill=BOTH, expand=True)

def icon_page():
    global input_file_entry  # Making input_file_entry a global variable for later use

    # Create StringVar instance for the icon file path
    icon_file_var = StringVar()

    def choose_icon_file():
        icon_path = filedialog.askopenfilename(title="Select Icon", filetypes=[("Icon files", "*.ico")])
        if icon_path:
            icon_file_var.set(icon_path)  # Set the selected file in the StringVar
            input_file_entry.delete(0, END)  # Clear the current entry
            input_file_entry.insert(0, icon_path)  # Set the selected file in the entry
            display_icon(icon_path)

    def display_icon(icon_path):
        icon_image = Image.open(icon_path)
        icon_image = ImageTk.PhotoImage(icon_image)
        display_label.configure(image=icon_image)
        display_label.image = icon_image

    icon_page_frame = CTkFrame(main_frame)

    input_file_entry = CTkEntry(icon_page_frame, textvariable=icon_file_var, placeholder_text="Path to the icon file", width=450)
    input_file_entry.pack(pady=50)

    input_file_button = CTkButton(icon_page_frame, text="Choose Icon .ico", command=choose_icon_file, width=150, corner_radius=40)
    input_file_button.pack()

    display_label = CTkLabel(icon_page_frame, text='')
    display_label.pack(pady=50)

    icon_page_frame.pack(fill=BOTH, expand=True)

this are pages and

def hide_bg():
    basic_indicate.configure(bg_color='transparent')
    assembly_indicate.configure(bg_color='transparent')
    icon_indicate.configure(bg_color='transparent')
    build_indicate.configure(bg_color='transparent')
    advanced_indicate.configure(bg_color='transparent')

def switch(widget,page):
    hide_bg()
    widget.configure(bg_color='blue')

    for fm in main_frame.winfo_children():
        fm.destroy()

    page()

this is the switching methodology and below is the main app code

set_appearance_mode("System")  # Modes: system (default), light, dark
app = CTk()

app.geometry("625x625")
app.iconbitmap('icon.ico')
app.title("FG_Crypter")

options_frame = CTkFrame(app)

basic_btn = CTkButton(options_frame,text="Basic",font=("Arial",13), width=125,command=lambda: switch(basic_indicate,basic_page))
basic_btn.place(x=0,y=0)

basic_indicate = CTkLabel(options_frame,text="",bg_color='blue',width=100,height=2)
basic_indicate.place(x=12,y=30)

icon_btn = CTkButton(options_frame,text="Icon",font=("Arial",13), width=125,command=lambda: switch(icon_indicate,icon_page))
icon_btn.place(x=125,y=0)

icon_indicate = CTkLabel(options_frame,text="", width=100,height=2)
icon_indicate.place(x=137,y=30)

Assembly_btn = CTkButton(options_frame,text="Assembly",font=("Arial",13), width=125,command=lambda: switch(assembly_indicate))
Assembly_btn.place(x=250,y=0)

assembly_indicate = CTkLabel(options_frame,text="",width=100,height=2)
assembly_indicate.place(x=262,y=30)

Advanced_btn = CTkButton(options_frame,text="Advanced",font=("Arial",13), width=125,command=lambda: switch(advanced_indicate))
Advanced_btn.place(x=375,y=0)

advanced_indicate = CTkLabel(options_frame,text="",width=100,height=2)
advanced_indicate.place(x=387,y=30)

Build_btn = CTkButton(options_frame,text="Build",font=("Arial",13), width=125,command=lambda: switch(build_indicate))
Build_btn.place(x=500,y=0)

build_indicate = CTkLabel(options_frame,text="",width=100,height=2)
build_indicate.place(x=512,y=30)
options_frame.pack(pady=5)
options_frame.propagate(False)
options_frame.configure(width=650,height=35)
main_frame = CTkFrame(app)
main_frame.pack(fill=BOTH,expand=True)
basic_page()
app.mainloop()

Solution

  • So when comes to building your basic_page when you set the input_file_var and output_file_var and give it a value of StringVar() you give it that value ( or in other words reset the saved data ) when you call the function again.

    You were very close and all that has to be done is move the:

    input_file_var = StringVar()
    output_file_var = StringVar()
    

    variables to place where they are called only once ( like the beginning ) so you can move them when you define the root objects like under the

    options_frame = CTkFrame(app)
    

    And if you are confused about this or other aspects of the code you are using feel free to ask and me or someone here can explain :)