How to define a tkinter variable as global?
<p>I'm trying to pull an integer from an <code>Entry</code>, and store it in a python variable to be used in various functions. Right now the problem I'm having is one of scope, wherein the <code>IntVar</code> is declared in one class, and the python variable is declared in another method in <code>class A</code> calls the IntVar to store it within a variable in <code>class A</code>.</p>

<p>Now I understand <em>why</em> the <code>IntVar</code> is not considered global. But when I try to put the <code>IntVar</code> anywhere <em>but</em> the class containing the gui, it returns an <code>__init__</code> error and quotes lines in the <code>tkinter</code> module.</p>

<p>I have also tried moving the functions to the UI class, but that gets ugly too because then I can't figure out how to call them from outside the <code>__init__</code> method, and putting them anywhere else causes a <code>NameError: name 'xxx' is not defined</code>.</p>

<p>Here's a quick and dirty mockup that illustrates the problem:</p>

<pre><code>from tkinter import *

class SampleObj(object):
    def __init__(self, pyvar):
        self.pyvar = 0

    def SampleFunc(self):
        self.pyvar = tkvar.get()
        self.pyvar += 1
        tkvar.set(self.pyvar)

Test = SampleObj(0)

class MainGui:
    def __init__(self, master):
        self.master = master
        self.master.title("I'm a test Gui!")

        tkvar = IntVar()

        mFrame = Frame(master)
        mFrame.pack()

        self.Entry = Entry(mFrame, textvariable=tkvar)
        self.Entry.pack()

        self.Label = Label(mFrame, text=tkvar.get())
        self.Label.pack()

        tkvar.trace("w", Test.SampleFunc())

root  = Tk()
my_gui = MainGui(root)
root.mainloop()
</code></pre>

<p>As mentioned above, I've tried moving things around quite a bit, declaring the <code>IntVar</code> in places I think would widen its scope, but every time it's either "not defined" or an issue with the variable not being initialized within the root (I think that's what the other error means, anyway).</p>

<p>I've googled the heck of this for about four hours now, and found nothing that points to a solution. I also consulted someone else in person who I would consider intermediate in Python (compared to me as a novice), and they didn't know what to tell me other than that it was clearly an issue with scope (which I already knew). So, any help pointing me in the right direction would be appreciated.</p>

<p><strong>EDIT:</strong> Tracebacks provided as requested.</p>

<p>Error for code as-is:</p>

<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File "REDACTED", line 34, in &lt;module&gt;
my_gui = MainGui(root)
  File "REDACTED", line 30, in __init__
tkvar.trace("w", Test.Magic())
  File "REDACTED", line 8, in Magic
self.pyvar = tkvar.get()
NameError: name 'tkvar' is not defined
</code></pre>

<p>Error when I try to move <code>tkvar = IntVar()</code> up and outside of all classes:</p>

<pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
  File "REDACTED", line 3, in &lt;module&gt;
tkvar = IntVar()
  File "/home/redacted/apps/thonny/lib/python3.5/tkinter/__init__.py",   line 351, in __init__
Variable.__init__(self, master, value, name)
  File "/home/redacted/apps/thonny/lib/python3.5/tkinter/__init__.py", line 233, in __init__
self._root = master._root()
AttributeError: 'NoneType' object has no attribute '_root'
</code></pre>