0

Python beginner with little tkinter or OOP experience.

Functional code from a tkinter youtube tutorial, which called all tkinter functions globally, copied and modified into seperate functions. Code now calls functions immediately upon start. Photo no longer displays if exit button is commented. What am I missing?


#simple glossary example demonstrating tkinter basics

#program broken by adding main and attempting to partition into seperate functions

from tkinter import *

dictionary_db = {
    'algorithm' : 'Algorithm: Step by step instructions to complete a task.',
    'bug' : 'Bug: Piece of code that causes a program to fail.'
}

photo_file_name = "logo.png"



#click function used by submit button
def click(text_entry, output):
    entered_text = text_entry.get()
    output.delete(0.0, END)
    try:
        definition = dictionary_db[entered_text]
    except:
        definition = "Sorry not an exisiting word. Please try again."
    output.insert(END, definition)

#exit function used by exit button
def close_window(window):
    window.destroy()
    exit()

#configure window
def config(window):

    # config
    window.title("My tkinter demo")
    window.configure(background="black")

    #create objects
    photo1 = PhotoImage(file=photo_file_name)
    photo_label = Label (window, image=photo1, bg="black")
    entry_label = Label(window, text="Enter the word you would like a definition for: ", bg="black", fg="white", font="none 12 bold")
    text_entry = Entry(window, width=20, bg="white")
    output = Text(window, width=75, height=6, wrap=WORD, background="white")
    submit_button = Button(window, text="SUBMIT", width=6, command=click(text_entry, output)) 
    definition_label = Label (window, text="\nDefinition", bg="black", fg="white", font="none 12 bold")
    exit_label = Label (window, text="click to exit", bg="black", fg="white", font="none 12 bold")
    exit_button = Button(window, text="Exit", width=6, command=close_window(window))

    #place objects
    photo_label.grid(row=0, column=0, sticky=E)
    entry_label.grid(row=1, column=0, sticky=W)
    text_entry.grid(row=2, column=0, sticky=W)
    submit_button.grid(row=3, column=0, sticky=W)
    definition_label.grid(row=4, column=0, sticky=W)
    output.grid(row=5, column=0, columnspan=2, sticky=W)
    exit_label.grid(row=6, column=0, sticky=W)
    exit_button.grid(row=7, column=0, sticky=W)

#main
def main():
    window = Tk()
    config(window)
    window.mainloop()

if __name__ == '__main__':
    main()

'''

1 Answers1

0

You're invoking the functions, when all you really should be doing is binding the functions to the command keyword argument.

Wrong:

def my_function():
    print("you clicked the button")

button = Button(command=my_function())

Right:

def my_function():
    print("you clicked the button")

button = Button(command=my_function)

However, the "Right" way of doing this won't work for you, since your functions accept parameters. In that case you can make a lambda:

def my_function(arg):
    print(arg)

button = Button(command=lambda: my_function("hello world"))
Paul M.
  • 10,481
  • 2
  • 9
  • 15