1

So I have this code

class createNewFile:
    def createNewFile(createFileEntry):
        string = StringVar()
        createFileEntry = Entry( CROW, textvariable = string)
        createFileEntry.pack()
        createFileEntry.focus_set()

    def saveNewFile(message):
        filename = createFileEntry.get()
        extention = filename
        target = open (extention, 'a')
        message = tkMessageBox.showinfo('What happened','Recived ' +getFileEntry+' as filename')


newFileButton = createNewFile()
newFileButton.createNewFile()
newFileButton.saveNewFile

newFileIcon = PhotoImage(file='icons/newFile.png')
createFileBtn = Button(toolbar, image = newFileIcon, command = newFileButton.createNewFile,relief='solid',background='white', border=0)
createFileBtn.pack(side=LEFT, padx=0,pady=0)

saveFileIcon = PhotoImage(file='icons/saveFile.png')
saveFileEntry = Button(toolbar,image = saveFileIcon, command = newFileButton.saveNewFile,relief='solid',background='white', border=0)
saveFileEntry.pack(side=LEFT, padx=0,pady=0)

and the idea is for the new file button to create a entry space to get the name and extension of the file, and the save button to write it.

I read here in stack overflow how to set up the class for you to be able to call a variable from one function to another, but it didn't work. What am I doing wrong?

4
  • 1
    indentation is wrong. The two functions inside the class must be indented for them to be part of the class. If this is not exactly the code you are working with, please copy it with the correct indentation. Commented May 20, 2015 at 19:12
  • 1
    Where is the indentation wrong? Commented May 20, 2015 at 20:38
  • Now it is ok. It was wrong in the first version. Commented May 20, 2015 at 20:45
  • Define "it didn't work". Include any errors too (in the question itself). Commented May 20, 2015 at 21:23

1 Answer 1

1

Here in the code shown above you are only creating local instances of the variables, create class variables if you want to use across classes, try using this: -

class createNewFile:
    def createNewFile(self): 
        string = StringVar()
        self.createFileEntry = Entry( CROW, textvariable = string)
        self.createFileEntry.pack()
        self.createFileEntry.focus_set()  

    def saveNewFile(self, message):
        filename = self.createFileEntry.get()  
        extention = filename 
        target = open (extention, 'a')  
        message = tkMessageBox.showinfo('What happened','Recived ' +getFileEntry+' as filename')

Also, I am unable to find getFileEntry in the source code you have provided. anything that you want to save in the class instance, save or edit it using self.variable_name. I hope I have answered your question.

Sign up to request clarification or add additional context in comments.

6 Comments

Also, thanks, I had changed the variable name and forgot to do so in the message
I'm sure it's something really silly that I'm not seeing. The error is TypeError: createNewFile() takes exactly 2 arguments (1 given). My code for instancing it is the same as in the question
Oh, this is because of def createNewFile(self, createFileEntry), Remove createFileEntry from the class definition, adding in edit, try removing it and running it. It is because, the function accepts createFileEntry as a parameter, and you are passing no parameters while calling the function
So I did that, and it came back to the original error: NameError: global name 'createFileEntry' is not defined
@NamitSingal I think you want self.createFileEntry. on the two last lines of the def createNewFile(self): block
|

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.