Python - Change variable outside function without return
<p>I just started learning Python and I ran into this problem. I want to set a variable from inside a method, but the variable is outside the method. </p>

<p>The method gets activated by a button. Then I want to get the value from that variable that I set when I press another button. The problem is that the value that I put inside a variable from inside the method doesn't stay. How would I solve this? </p>

<p>The code is underneath. <code>currentMovie</code> is the variable I try to change. When I press the button with the method <code>UpdateText()</code>, it prints out a random number like it is supposed to. But when I press the button that activates <code>UpdateWatched()</code> it prints out 0. So I am assuming the variable never gets set.</p>

<pre><code>import random
from tkinter import *

currentMovie = 0

def UpdateText():
    currentMovie = random.randint(0, 100)
    print(currentMovie)

def UpdateWatched():
    print(currentMovie)

root = Tk()
root.title("MovieSelector9000")
root.geometry("900x600")
app = Frame(root)
app.grid()
canvas = Canvas(app, width = 300, height = 75)
canvas.pack(side = "left")
button1 = Button(canvas, text = "SetRandomMovie", command = UpdateText)
button2 = Button(canvas, text = "GetRandomMovie", command = UpdateWatched)
button1.pack(anchor = NW, side = "left")
button2.pack(anchor = NW, side = "left")
root.mainloop()
</code></pre>

<p>Here's a simple (python 2.x) example of how to 1 <em>not</em> use globals and 2 use a (simplistic) domain model class. </p>

<p>The point is: you should first design your domain model independently from your user interface, then write the user interface code calling on your domain model. In this case your UI is a Tkinter GUI, but the same domain model should be able to work with a command line UI, a web UI or whatever.</p>

<p>NB : for python 3.x, replace <code>Tkinter</code> with <code>tkinter</code> (lowercase) and you can't get rid of the <code>object</code> base class for <code>Model</code>.</p>

<pre><code>import random
from Tkinter import *


class Model(object):
    def __init__(self):
        self.currentMovie = 0

    def UpdateCurrentMovie(self):
        self.currentMovie = random.randint(0, 100)
        print(self.currentMovie)

    def UpdateWatched(self):
        print(self.currentMovie)

    def ExampleWithArgs(self, arg):
        print("ExampleWithArg({})".format(arg))


def main():
    model = Model()
    root = Tk()
    root.title("MovieSelector9000")
    root.geometry("900x600")
    app = Frame(root)
    app.grid()
    canvas = Canvas(app, width = 300, height = 75)
    canvas.pack(side = "left")
    button1 = Button(canvas, text = "SetRandomMovie", command=model.UpdateCurrentMovie)
    button2 = Button(canvas, text = "GetRandomMovie", command=model.UpdateWatched)
    button3 = Button(canvas, text = "ExampleWithArg", command=lambda: model.ExampleWithArgs("foo"))
    button1.pack(anchor = NW, side = "left")
    button2.pack(anchor = NW, side = "left")
    button3.pack(anchor = NW, side = "left")
    root.mainloop()

if __name__ == "__main__":
    main()
</code></pre>

python