Search code examples
pythontkinterundefined

Changing a Frame from an original window to another


I have this function that is generating a button every time another function is called and placing the new button into a new window. I've successfully implemented this code into my main program except for one part, the buttons will not generate in any other window except my main window. The code is a little bit longer, so I apologize.

import tkinter as tk
from pathlib import Path

def window2():
    editor = tk.Toplevel()
    editor.title("Window 2")
    
def add_buttons(*items):
    for item in items:
        auto_button = tk.Button(button_frame, text=item)
        auto_button.grid(column=1)

def get_items(path):
    if path.exists():
        with open(path, "r") as f:
            data = f.read().strip().splitlines()
            return data
    return []

def save_items(path, items):
    with open(path, "w") as f:
        f.write("\n".join(items))

def submit():
    global items

    # get the text and append it to our global list of button text
    text = entry.get()
    items.append(text)

    # create the button
    add_buttons(text)

    # save the list of items
    save_items(path, items)

root = tk.Tk()
button_frame = tk.Frame(root)
entry = tk.Entry(root)
submit = tk.Button(root, text="Submit", command=submit)

entry.grid(row=0, column=1)
submit.grid(row=1, column=0, columnspan=2)
button_frame.grid(column=1)

window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)

# initialize our list of buttons from a file
path = Path("buttons.txt")
items = get_items(path)
add_buttons(*items)

root.mainloop()

I've tested this in 3 different programs now, all with the same pattern of the button properly operating in the main startup window, but won't allow me to call the function to operate in a different window as seen above in button_frame = tk.Frame(window). The code above is almost verbatim Bryan Oakley's, credit to him, I went back into the code sample he sent me to show my minimal with the same error. Each time this function is called it gives a undefined (window in this exmaple) error. I know this can't be a hard fix but for the love of me I haven't be able to figure it out.


Solution

  • Here is the complete script. This code works!

    from pathlib import Path
    import tkinter as tk
    
    def window2():
        window.deiconify()
        window.lift()
        window.title("Window 2")
        window.focus_force()
        
    def add_buttons(*items):
        for item in items:
            auto_button = tk.Button(button_frame, text=item)
            auto_button.grid(column=1)
    
    def get_items(path):
        if path.exists():
            with open(path, "r") as f:
                data = f.read().strip().splitlines()
                return data
        return []
    
    def save_items(path, items):
        with open(path, "w") as f:
            f.write("\n".join(items))
    
    def submit():
        global items
    
        # get the text and append it to our global list of button text
        text = entry.get()
        items.append(text)
    
        # create the button
        add_buttons(text)
    
        # save the list of items
        save_items(path, items)
    
    root = tk.Tk()
    window = tk.Toplevel()
    
    button_frame = tk.Frame(window)
    entry = tk.Entry(root)
    submit = tk.Button(root, text="Submit", command=submit)
    
    entry.grid(row=0, column=1)
    submit.grid(row=1, column=0, columnspan=2)
    button_frame.grid(column=1)
    
    window2_btn = tk.Button(root, text="Take me to window 2", command=window2).grid(row=2, column=0,)
    
    # initialize our list of buttons from a file
    path = Path("buttons.txt")
    items = get_items(path)
    add_buttons(*items)
    
    root.mainloop()