0

I was learning tkinter from a youtube video and this is the code:

import tkinter as tk

def click():
    entered_text = textentry.get()  
    output.delete(0.0, tk.END) 
    try:
        definition = my_compdictionary[entered_text] 
    except:
        definition = "sorry there is no word like that please try again"
    output.insert(tk.END, definition)  


# main
window = tk.Tk()
window.title("My Computer Science Glossary")
window.configure(background="black")

# adding photo
photo1 = tk.PhotoImage(file="plusimg.png")
a = tk.Label(window, image=photo1, bg="black")  
a.grid(row=0, column=0, sticky=tk.W)  

# create label/text
lab = tk.Label(window, text="Enter the word you would like a definition for:", bg="black",  
               fg="white", font="none 12 bold").grid(row=1, column=0, sticky=tk.W)          

# text entry box
textentry = tk.Entry(window, width=20, bg="white")
textentry.grid(row=2, column=0, sticky=tk.W)

# button
tk.Button(window, text="SUBMIT", width=6).grid(row=3, column=0, command=click(), sticky=tk.W)  

# another label
tk.Label(window, text="\nDefinition:", bg="black", fg="white", font="none 12 bold").grid(row=4, column=0, sticky=tk.W)

# output text box
output = tk.Text(window, width=75, height=6, wrap=tk.WORD, background="white")  
output.grid(row=5, column=0, columnspan=2, sticky=tk.W)

# the dictionary
my_compdictionary = {
    'algorithm': 'Step by step instructions to complete a task', 'bug': 'piece of code that causes a program to fail'
    }

window.mainloop()

The issue appears to be occurring in the click() function. The error I get is 'output' is not defined
The error message also prints this line but doesn't say whats wrong with it:

tk.Button(window, text="SUBMIT", width=6).grid(row=3, column=0, command=click(), sticky=tk.W)    

The program is suppose to allow me to input a word and it would give the definition for it as I have it set at my_compdictionary

Guy I'm learning from: https://www.youtube.com/watch?v=_lSNIrR1nZU

Cherry
  • 191
  • 1
  • 3
  • 9
  • 2
    Firstly, the `command` should be passed to the `Button` instead of `grid`. Secondly, you need to pass it as a reference, so putting it all together: `tk.Button(window, text="SUBMIT",command=click, width=6).grid(row=3, column=0, sticky=tk.W)` – Henry Yik Apr 04 '19 at 03:55
  • Another thing, the reason why the ```tk.Button(window, text="SUBMIT", width=6).grid(row=3, column=0, command=click(), sticky=tk.W) ``` was there is because it referenced to the line the error was one. –  Apr 04 '19 at 05:02
  • @BryanOakley, this isn't a duplicate. In the other question, the OP added an argument to the command. In this one, he doesn't do that. He simply just passes it. As mentioned by , he passed the command in the grid, so this isn't a duplicate. –  Apr 04 '19 at 05:03
  • @Programmer: whether there's an argument or not is irrelevant. The root of the problem is that this code is immediately calling the function and passing the result to the `command` parameter. – Bryan Oakley Apr 04 '19 at 07:48
  • @BryanOakley In that question, he had trouble with arguments. In this one, he has trouble with the entire function. There is a difference. \ –  Apr 04 '19 at 16:59
  • But the root of the problem is the same, that is true. –  Apr 04 '19 at 17:01

0 Answers0