Search code examples
python-3.xtkintertkinter-button

Tkinter open window on command


I'm brand new to Tkinter and UI doesn't click for me at all. I am trying to make a main window with multiple buttons, some of which open a secondary window with new functionality. for some reason when I run my code it just opens all my windows at once (currently just 2) and the button I bonded doesn't do anything (also the bind command just causes the 2nd window to open again). I looked it up here but all the tickets I find and try their solutions for some reason don't work for me, so sorry for yet another post on the topic. Here is my code for the UI portion (all my main code does at this time is just call the function start_window()):

import tkinter as tk




def start_window(files=0):
    window = tk.Tk()
    window.title("Desk Cleaner")
    frm_left = tk.Frame(width=25, height=15, padx=5, pady=5)
    frm_right = tk.Frame(width=25, height=15, padx=5, pady=5)
    frm_right_inner = tk.Frame(master=frm_right)

    lbl_available_files_to_delete = tk.Label(text="{} available files to delete".format(files),width=25,
                                              master=frm_right_inner, relief=tk.GROOVE)
    btn_refresh_file_counter = tk.Button(text="0", width=2, master=frm_right_inner)
    btn_run_cleaner = tk.Button(text="Run Cleaner", width=25, height=2, master=frm_left)
    btn_create_template = tk.Button(text="Create Template", width=25, height=2, master=frm_left,
                                    command=create_template_window())
    btn_delete_template = tk.Button(text="Delete Template", width=25, height=2, master=frm_left)

    btn_create_template.bind(create_template_window())

    lbl_available_files_to_delete.pack(side=tk.LEFT)
    btn_refresh_file_counter.pack(side=tk.RIGHT)
    btn_run_cleaner.pack()
    btn_create_template.pack()
    btn_delete_template.pack()

    frm_left.pack(side=tk.LEFT)
    frm_right.pack(side=tk.RIGHT)
    frm_right_inner.pack()
    window.mainloop()


def create_template_window():
    create_template_window = tk.Tk()
    create_template_window.title("New Template")

    frm_top = tk.Frame(master= create_template_window,padx=5, pady=5)
    frm_bot = tk.Frame(master= create_template_window, padx=5, pady=5)
    frm_inner_bot = tk.Frame(master=frm_bot, padx=5, pady=5)

    label = tk.Label(text="Template Name", master=frm_top).pack()
    entry = tk.Entry(master=frm_top).pack()
    submit_button = tk.Button(text="Submit", width=25, height=2, master=frm_inner_bot).pack(side=tk.LEFT)
    cancel_button = tk.Button(text="Cancel", width=25, height=2, master=frm_inner_bot).pack(side=tk.RIGHT)

    frm_top.pack()
    frm_bot.pack()
    frm_inner_bot.pack()

I've attempted to play around with a side file tinkering with it a bit and trying different solutions found on this site, yet none of them seemed to help with my issue. At this time im mostly following this tutorial: https://realpython.com/python-gui-tkinter/#making-your-applications-interactive


Solution

  • This is just a case of passing in the result of a function call, not the function itself. The () should be removed from bind/command.

    btn_create_template.bind(create_template_window) tells it to run the passed-in function, but btn_create_template.bind(create_template_window()) tells it to call the function itself first, get the output, and set that as the bound function.

    You can use lambda if you want to pass in specific arguments.