Setting global variables between classes with tKinter
<p>I am currently struggling to use input derived from another class (which uses tkinter) to alter outputs in a different class. This is my code:</p>

<pre><code>#there are many of these defs, this is just one i used as an example

    def command_ssumowrestler(self):
        self.master.withdraw()
        toplevel = tk.Toplevel(self.master)
        toplevel.geometry("30x70")
        app=ClassConfirmation(toplevel)
        global cs_hobby
        cs_hobby = 'sumo_wrestler'

class ClassConfirmation:
    def __init__ (self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        confirmclass_label = tk.Label(master, width = 20, fg = 'blue',text= "Do You Wish to Proceed as:")
        confirmclass_label.pack()

#here i use the variable defined and set globally in the previously block, which always leaves me with an error saying cs_hobby is not defined 

        classdescription_label = tk.Label(master, width =50, text='test ' + cs_hobby + ' hello')
        classdescription_label.pack()
        self.confirmclassButton = tk.Button(self.frame, text = 'Confirm', width = 20, command = self.close_windows)
        self.confirmclassButton.pack()
        self.frame.pack()
    def close_windows (self):
        self.master.withdraw()
        app=dragonrealmP1
#this just opens a different class
</code></pre>

<p><code>global</code> doesn't create variable - it only informs function/method to use global variable instead of local variable. First you have to create this variable before classes.</p>

<hr>

<p>BTW: if you use classes so why don't you use in first class</p>

<pre><code>self.cs_hobby = 'sumo_wrestler'
</code></pre>

<p>and in <code>ClassConfirmation</code></p>

<pre><code>sone_object_name.cs_hobby 
</code></pre>

<p>or</p>

<pre><code>app = ClassConfirmation(toplevel, 'sumo_wrestler')


class ClassConfirmation:
    def __init__ (self, master, cs_hobby):

        # here use cs_hobby 
</code></pre>

<hr>

<p><strong>EDIT:</strong></p>

<p>First: <code>global cs_hobby</code> doesn't create global variable. It only inform function to use global <code>cs_hobby</code> instead of local <code>cs_hobby</code></p>

<p>to work with <code>global</code> you need </p>

<pre><code>cs_hobby = ""   # create global variable (outside classes)


def command_ssumowrestler(self):
    global cs_hobby  # inform function to use global variable

    # rest 

    cs_hobby = 'sumo_wrestler' # set value in global variable

    app = ClassConfirmation(toplevel)


class ClassConfirmation:
    def __init__ (self, master):
        global cs_hobby    # inform function to use global variable

        # rest 

        print(cs_hobby)    # get value from global variable
        cs_hobby = 'tenis' # set value in global variable
</code></pre>

<p>to work without global variable you need </p>

<pre><code>def command_ssumowrestler(self):
     self.cs_hobby = 'sumo_wrestler' # create local variable

     app = ClassConfirmation(toplevel, self.cs_hobby) # send local value to another class

     self.cs_hobby = app.cs_hobby # receive value from another class


class ClassConfirmation:
    def __init__ (self, master, hobby):
        self.cs_hobby = hobby # create local variable and assign value 

        # rest 

        print(self.cs_hobby)     # use local value
        self.cs_hobby = 'tenis'  # use local value
</code></pre>

python class variables tkinter