Set string in 'A' python script from 'B' python script
<p>I have two scripts: </p>

<p>A.py (is TK window)</p>

<pre><code>def function():
    string = StringVar()
    string.set("Hello I'm A.py")
</code></pre>

<p>From B.py I wish change string that appear in Tk window.</p>

<pre><code>def changestring():
    string.set("Hello I'm B.py")
</code></pre>

<p>Obviously don't work! How I can change string from another python script? </p>

<p>Variables have scopes. You cannot access variables which are in the scope of one function from another function.</p>

<p>There has to be some common point in the code which "knows" about A and about B. That code should pass the variables from one to the other.</p>

<p>Based on this comment:</p>

<blockquote>
  <p>A.py is the graphic and B is the core with a infinite loop that listen the USB interrupt</p>
</blockquote>

<p>I would say that you need two objects implementing the two functionalities, let's call them "graphic" and "usb". One of them has to "know" about the other. Either "graphic" should observe "usb", or "usb" should update "graphic".</p>

<p>For example:</p>

<pre><code># possibly in A.py:

class Graphic(object):
    def __init__(self):
        self.string = StringVar()

    def function(self):
        self.string.set("Hello I'm A.py")


# possibly in B.py:

class USB(object):
    def __init__(self, graphic):
        self.graphic = graphic

    def changestring(self):
        self.graphic.string.set("Hello I'm B.py")


# somewhere:

from A import Graphic
from B import USB

def main():
    graphic = Graphic()
    usb = USB(graphic)
    #...
    usb.changestring()
</code></pre>

python function variables tk