How do I create a button in Python Tkinter to increase integer variable by 1 and display that variable?
Question: I am trying to create a Tkinter program that will store an int variable, and
increase that int variable by 1 each time I click a button, and then display
the variable so I can see that it starts out as 0, and then each time I click
the button it goes up by 1. I am using python 3.4.

    
    
    import sys
    import math
    from tkinter import *
    
    root = Tk()
    root.geometry("200x200")
    root.title("My Button Increaser")
    
    counter = 0
    def nClick():
        counter + 1
    
    def main_click():
        mLabel = Label(root, text = nClick).pack()
    
    
    mButton1 = Button(text = "Increase", command = main_click, fg = "dark green", bg = "white").pack()
    
    root.mainloop()
    


Answer: You could use Tkinter variables. They are specially useful when you need to
modify a data that other widgets might interact with. Here is a look alike
code to the one in the question, but instead of defining `counter` as a normal
variable, it is a variable from Tkinter.

    
    
    import tkinter
    import sys
    
    root = tkinter.Tk()
    root.geometry("200x200")
    root.title("His Button Increaser")
    
    counter = tkinter.IntVar()
    
    def onClick(event=None):
        counter.set(counter.get() + 1)
    
    tkinter.Label(root, textvariable=counter).pack()
    tkinter.Button(root, text="Increase", command=onClick, fg="dark green", bg = "white").pack()
    
    root.mainloop()
    

Instead of passing the value this variable holds to the `text` attribute of
the `Label`, we assign the variable to `textvariable` attribute, so when the
value of the variable gets updated, `Label` would update the displayed text
accordingly.

When you want to change the value of the variable, you'd need to call the
`set()` method of the variable object (see `onClick`) instead of assigning the
value directly to it.

