title: How to make buttons call functions
body: <p>After defining my word count method thanks to another member, I am now creating a GUI to go along with it. I have created three buttons, one for browsing for a file, one for counting the words and lines in a file, and one for exiting.</p>
  
  <p>My question is, how do I make these buttons do things? I am trying to make the "Browse for a file" run the <code>filename = fileopenbox()</code> line, and the "Count" button run the <code>word_count()</code> method.</p>
  
  <p>Here is what the code looks like:</p>
  
  <pre><code>from tkinter import *
  from easygui import fileopenbox
  
  root = Tk()
  root.title("Word Counter")
  root.geometry("500x500")
  
  app = Frame(root)
  app.grid()
  button1 = Button(app, text = "Browse for a file")
  button1.grid()
  
  button2 = Button(app)
  button2.grid()
  button2.configure(text ="Count the file")
  
  button3 = Button(app)
  button3.grid()
  button3["text"] = "Exit"
  
  root.mainloop()
  
  def word_count(filename):
      filename = fileopenbox()
      if not filename.endswith(('.txt', '.py', '.java')):
          print('Are you trying to annoy me? How about giving me a TEXT or SOURCE CODE file, genius?')
          return
  
      with open(filename) as f:
          n_lines = 0
          n_words = 0
          for line in f:
              n_lines += 1
              n_words += len(line.split())
      print('Your file has {} lines, and {} words'.format(n_lines, n_words))
  </code></pre>