1

This code is part of a bigger program that uses the google Sheets API to get data from a cloud database (not really relevant, but a bit of context never hurt!)

I have this black of code in one python file named 'oop.py'

class SetupClassroom:
    def __init__(self, arraynumber='undefined', tkroot='undefined'):
        self.arraynumber = arraynumber
        self.tkroot = tkroot

    def setarraynumber(self, number):
        from GUI_Stage_3 import showclassroom
        self.arraynumber = number
        print ('set array number:', number)

        showclassroom()

    def settkroot(self, tkrootinput):
        self.tkroot = tkrootinput

self.tkroot has been assigned by another part of the code. This bit works, as I have already tested that it is being assigned, however, when I call 'self.tkroot' in another another file like this

def showclassroom():
    from oop import SetupClassroom
    username = current_user.username
    classnumber = getnumberofuserclassrooms(username)
    if SetupClassroom.arraynumber > classnumber:
        errorwindow('you are not enrolled in that many classrooms!')
    else:

        classtoget = SetupClassroom.arraynumber
        print('classtoget:', classtoget)
        root = SetupClassroom.tkroot
        name_label = Label(root, text=classtoget)

        getclassroom(username, classtoget)
SetupClassroom = SetupClassroom

I get this error

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/GUI_Stage2_better.py", line 176, in <lambda>
    l0 = ttk.Button(teacher_root, text=button0text, command=lambda: (SetupClassroom.setarraynumber(SetupClassroom, number=button0text), SetupClassroom.settkroot(SetupClassroom, 'teacher_root')))
  File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/oop.py", line 99, in setarraynumber
    showclassroom()
  File "/Users/jonathansalmon/PycharmProjects/Coursework_GUI/GUI_Stage_3.py", line 29, in showclassroom
    root = SetupClassroom.tkroot
AttributeError: type object 'SetupClassroom' has no attribute 'tkroot'

I tried setting it up in the python console and it worked, so I have no idea what the problem is. If anyone could help, it would be very much appreciated Thanks! John

2
  • 1
    You need to make an instance of the class. I'm wondering why SetupClassroom.arraynumber didn't raise an error first. Commented Mar 29, 2019 at 11:11
  • ah, sorry, If understand what you mean, I did accidentally leave that out of the main question, I am adding that now (SetupClassroom = SetupClassroom) Commented Mar 29, 2019 at 11:15

1 Answer 1

1

You should create an instance of class, it will create the attribute in __init__, self.tkroot is the attribute of instance not class:

setupClassroom = SetupClassroom()
print(setupClassroom.tkroot)

Hope that will help you.

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

Comments

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.