0

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:

#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

1 Answer 1

1

global 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.


BTW: if you use classes so why don't you use in first class

self.cs_hobby = 'sumo_wrestler'

and in ClassConfirmation

sone_object_name.cs_hobby 

or

app = ClassConfirmation(toplevel, 'sumo_wrestler')


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

        # here use cs_hobby 

EDIT:

First: global cs_hobby doesn't create global variable. It only inform function to use global cs_hobby instead of local cs_hobby

to work with global you need

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

to work without global variable you need

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
Sign up to request clarification or add additional context in comments.

4 Comments

I don't understand what this means and how this works, can you explain it please?
see new text in answer
so when you put words in the brackets after def, does that import variables from outside the class/def?
when you have def fun(arg1, arg2) and use fun(123, "Hello") then fun creates local variables arg1 and arg2 and assigns arg1=123 and arg2="Hello". It doesn't mean that fun import external variables arg1 and arg2. When you use fun(global_variable_1, global_variable_2) then fun creates local arg1 arg2 again and assigns arg1=global_variable_1, arg2=global_variable_2`.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.