To gain function the button for generating values
Question: The python code I've pasted below generates the values 0 through 100 and
displays them in a textbox-like shell console. In addition to this, I added a
button deep of textbox labelled 'PRESS'. I would like the process of
generating the values 0 through 100 to begin when I click the 'PRESS' button.
I have not been able to set this up successfully. Can you help me please?

    
    
    #!/usr/bin/env python
    import Tkinter as tk
    import sys
    from threading import *
    
    class Console(tk.Frame):
        def __init__(self,parent=None):
           tk.Frame.__init__(self, parent)
           self.parent = parent
           sys.stdout = self
           sys.stderr = self
           self.createWidgets()
           self.consoleThread = ConsoleThread()
           self.after(100,self.consoleThread.start)
    
        def write(self,string):
    
           self.Text.insert('end', string)
           self.Text.see('end')
    
        def createWidgets(self):
    
           self.Text = tk.Text(self.parent, wrap='word',height=38,width=115, bg='white', fg = "blue",font="Verdana 9 bold")
           self.Text.grid()
    
           self.b = tk.Button(text="PRESS")
           self.b.grid()
    
    class ConsoleThread(Thread):
        def __init__(self):
           Thread.__init__(self)
        def run(self):
           def values():
              print 'TEST'
              for i in range(101):
               print i
           x=values()
           print x
    if __name__ == '__main__':
          root = tk.Tk()
          bas=root.title('Test')
          root.geometry('1000x700')
          root.config(background="light blue")
          main_window = Console(root)
          main_window.mainloop()
          try:
            if root.winfo_exists():
             root.destroy()
          except:
            pass
    


Answer: To achieve what you're looking for, you need to attach the function that you'd
like to call to the button you want to use. This is called a **callback**.

Luckily, TKinter makes this pretty easy to do - when constructing a button,
instead of writing:

    
    
    my_button = tk.Button(text="Click Me!")
    

You can pass another keyword argument to the constructor, `command`, which is
a function that will be called when the button is activated. That would look
something like this:

    
    
    def callback_message():
        print("I just got called back!")
    my_button = tk.Button(text="Click Me!", command=callback_message)
    

Now, whenever you click on `my_button`, `callback_message` will run!

In your specific case, I'd also move `values()` out of `ConsoleThread.run()`;
that way, it won't be called when the `ConsoleThread` is initialized.

