User input from GUI in Python 3.5
Question: So, I'm pretty new to Python, but I am having trouble handling the variables
once they are placed in a class.

The following code works fine when there is no surrounding class, but once I
add it I get the error:

    
    
    NameError: name 'someName' is not defined
    

Which occurs on the 3rd line

    
    
    text = "You have entered " + someName.get()
    

Here's the code:

    
    
    class GUI:
        def changeLabel():
            text = "You have entered " + someName.get()
            labelText.set(text)
            someName.delete(0, END)
            someName.insert(0, "You've clicked!")
            return
    
        app = Tk()
        app.title("GUI Test")
        app.geometry('450x300')
    
        labelText = StringVar()
        labelText.set("Click when ready")
        label1 = Label(app, textvariable=labelText, height=4)
        label1.pack()
    
        userInput = StringVar(None)
        someName = Entry(app, textvariable=userInput)
        someName.pack()
    
        button1 = Button(app, text="Click Here", width=20,command=changeLabel)
        button1.pack(side='bottom',padx=15,pady=15)
    
        app.mainloop()
    
    GUI #calling the class to run
    

Any help would be greatly appreciated.


Answer: You have a some errors in your code, and aren't using classes in the way that
you should be. I've modified your code and commented the new lines so you
understand what's happening. I've added `self` references to all the text
variables so they can be accessed properly.

    
    
    from tkinter import *
    
    class GUI:
            #set these so that they are able to be used by the whole class
            labelText = ""
            userInput = "" 
    
            #you should have an init method for your classes and do all the setup here
            def __init__(self,master):   
                    self.labelText = StringVar()
                    self.userInput = StringVar()
    
                    #you should pack things into a frame
                    frame = Frame(master)     
                    frame.pack()
    
                    self.labelText.set("Click when ready")
                    label1 = Label(frame, textvariable=self.labelText, height=4)
                    label1.pack()
    
                    someName = Entry(frame, textvariable=self.userInput)
                    someName.pack()
    
                    button1 = Button(frame, text="Click Here", width=20,command=self.changeLabel)
                    button1.pack(side='bottom',padx=15,pady=15)
    
            def changeLabel(self):
                    text = "You have entered " + self.userInput.get()
                    self.labelText.set(text)
                    self.userInput.set("You've clicked!")
                    return
    
    #create the app before you call the GUI.
    app = Tk()
    app.title("GUI Test")
    app.geometry('450x300')
    
    # when you create the class, you need to assign it to a variable
    applet = GUI(app) #calling the class to run
    app.mainloop()
    

