0

I am using Tkinter and trying to call a function within a class, but not getting it to work properly.

class Access_all_elements:

        def refSelect_load_file(self): 

            self.reffname = askopenfilename(filetypes=(("XML files", "*.xml"),
                                                       ("All files", "*.*") ))
            if self.reffname:
                fileOpen = open(self.reffname)

        refSelect = Button(topFrame, text="Open Reference File", 
                    command=lambda:refSelect_load_file(self), bg = "yellow")
        refSelect.grid(row =1, column=1)

Error:

On executing above command, on pressing the button I get following error:

NameError: global name 'refSelect_load_file' is not defined

What I tried:

I tried calling a function using tkinter's generic approach which is not working for me.

refSelect = Button(topFrame, text="Open Reference File", 
                        command=refSelect_load_file, bg = "yellow")
refSelect.grid(row =1, column=1)

This throws me error:

TypeError: refSelect_load_file() takes exactly 1 argument (0 given)

Can you guys suggest me something here?

7
  • 2
    It seems unusual to me to create widgets in the class scope, rather than inside a method in the class, such as __init__. If you were in a method, you could do command=self.refSelect_load_file. Commented Jul 31, 2015 at 15:38
  • @Kevin Thanks, I didn't know that. I will try it now. Commented Jul 31, 2015 at 15:39
  • @Digvijayad Hey i tried it, but button got disappeared. after putting entire code in _init_ Commented Jul 31, 2015 at 15:49
  • Your code shows refSelect_load_file (lowercase leading "r") but the error message shows RefSelect_load_file (uppercase leading "r") Commented Jul 31, 2015 at 15:59
  • @BryanOakley Thanks for pointing. In my code the naming of classes is not aligned properly. I was suggested in last post to use proper name structure for classes and function. so the error you see is from names I used in my code. Sorry for that. I edited my post though Edit: it was you i see now that suggested in last post :) thanks for that. Commented Jul 31, 2015 at 16:07

1 Answer 1

1

Your problem will be solved when you call the function using self.

refSelect = Button(topFrame, text="Open Reference File", 
                    command=self.refSelect_load_file, bg = "yellow")

Edit
Try this.

class Access_all_elements():
    def __init__(self):
        refSelect = Button(topFrame, text="Open Reference File", 
                command=lambda:self.refSelect_load_file, bg = "yellow")
        refSelect.grid(row =1, column=1)


    def refSelect_load_file(self): 

        self.reffname = askopenfilename(filetypes=(("XML files", "*.xml"),
                                                   ("All files", "*.*") ))
        if self.reffname:
            fileOpen = open(self.reffname)

Final Edit

from tkinter import *
from tkinter import filedialog


class Access_all_elements():

    def __init__(self):
        refSelect = Button(root, text="Open Reference File",command=self.refSelect_load_file, bg = "yellow")
        refSelect.grid(row =1, column=1)

    def refSelect_load_file(self): 
        self.reffname = filedialog.askopenfilename(filetypes=(("XML files", "*.xml"), ("All files", "*.*") ))
        if self.reffname:
            fileOpen = open(self.reffname)

root = Tk()
Access_all_elements()
root.mainloop()
Sign up to request clarification or add additional context in comments.

7 Comments

But self isn't a valid reference unless you're inside a method that has a self parameter. He's creating the button directly inside the class.
@digvijayad it throws error 'self' is not defined
@Digvijayad my button disappeared now. I did same.
I forgot to put the self. in the edit... try it now. If there is error let me know the error.
It shows no error. but button from GUI is gone. It disappeared.
|

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.